绝地求生米拉马延迟高_知道无限的人:编码拉马努詹的出租车

绝地求生米拉马延迟高

by Geoffrey Bourne

杰弗里·伯恩(Geoffrey Bourne)

知道无限的人:编码拉马努詹的出租车 (The Man Who Knew Infinity: Coding Ramanujan’s Taxi)

Have you see the movie (or read the book) The Man Who Knew Infinity?

您看过电影(或看过书) 《无尽的男人》吗?

This new movie — which stars Dev Patel and Jeremy Irons — explores Indian mathematician Srinivasa Ramanujan and his profound understanding, ingenuity, and love of math.

这部新电影由戴维·帕特尔(Dev Patel)和杰里米·艾恩斯(Jeremy Irons)饰演,探讨了印度数学家Srinivasa Ramanujan及其对数学的深刻理解,独创性和热爱。

The film inspired me on both an intellectual and emotional level. But what really drew my attention was a particular five second scene.

这部电影在思想和情感上都启发了我。 但是真正引起我注意的是一个特定的五秒钟场景。

The scene takes place in 1918. Ramanujan‘s mentor and friend G.H. Hardy quips that he had just taken taxi number 1729 and finds the number “a rather dull one.”

现场发生在1918年。拉马努詹的良师益友GH Hardy嘲笑他刚乘过1729号出租车,发现这个号码“相当呆板”。

Ramanujan passionately replies, “No, Hardy, it’s a very interesting number! It’s the smallest number expressible as the sum of two cubes in two different ways.”

Ramanujan热情地回答:“不,Hardy,这是一个非常有趣的数字! 它是用两种不同方式表示为两个多维数据集之和的最小数字。”

Ramanujan was able to see beyond the simple taxi cab number and into the depths of the expression behind it: a³ + b³ = c³ + d³…better known as Ramanujan’s Taxi. I thought this problem was fascinating and wondered how the code implementation would look. Little did I realize there were many optimization layers to this algorithm onion.

Ramanujan不仅可以看到简单的出租车号码,还可以看到其背后的表情:a³+b³=c³+d³………更被称为Ramanujan的出租车 。 我认为这个问题令人着迷,并想知道代码实现的外观。 我几乎没有意识到该算法洋葱有很多优化层。

实施Ramanujan出租车的第一步 (First Crack at Implementing Ramanujan’s Taxi)

I started with a straight forward implementation written in Scala. The code, with performance timings, can be found on GitHub:

我从使用Scala编写的直接实现开始。 带有性能计时的代码可以在GitHub找到

We begin with a brute-force implementation by looping though all combinations to find where a³ + b³ = c³ + d³. We achieve O(n⁴) performance because of the four loops used to calculate all values of a³, b³, c³, and d³ equal or less than parameter n, which bounds our search field.

我们通过循环遍历所有组合以找出a³+b³=c³+d³的方式开始于蛮力实施。 由于四个循环用于计算等于或小于参数n的所有a³,b³,c³和d³值,因此我们实现了O(n⁴)性能,这限制了我们的搜索范围。

This brute-force implementation, with O(n⁴) performance, kinda sucks. So, how can we do better?

这种具有O(n⁴)性能的强力实现有点糟。 那么,我们如何才能做得更好?

我们可以做得更好 (We Can Do Better)

First question to ask is: do we always need to calculate all the values of a³, b³, c³, and d³? Remember, the equation we are using is a³ + b³ = c³ + d³. If we solve for d³, we get d³ = a³ + b³ - c³. Thus, once we know a³, b³, and c³, we can calculate the value of d³ directly instead looping through all values of d³.

首先要问的是:我们是否总是需要计算a³,b³,c³和d³的所有值? 记住,我们使用的方程是a³+b³=c³+d³。 如果求解d³,则得到d³=a³+b³-c³。 这样,一旦我们知道a³,b³和c³,就可以直接计算d³的值,而不必遍历所有d³的值。

My next implementation, again in Scala, replaces the fourth loop with the calculation d³ = a³ + b³ — c³:

我的下一个实现也是在Scala中,用计算值d³=a³+b³—c³代替了第四个循环:

The 2nd version has O(n³) performance since we get to skip that final loop. Neat!

由于我们跳过了最后一个循环,因此第二个版本具有O(n³)性能。 整齐!

第三次魅力 (Third Time’s A Charm)

We’re not done yet. There is a third, and the best yet, enhancement to consider. What if we don’t need to solve for all values of not only d³, but c³ too? A few things to understand:

我们还没有完成。 需要考虑的第三项也是最好的增强。 如果我们不仅需要求解d³,还需要求解c³的所有值,该怎么办? 需要了解的几件事:

  1. If we calculate all values of a³ and b³ equal to or less than n, we essentially have all possible values of not only a³ and b³, but also c³ and d³.

    如果我们计算出所有等于或小于n的a³和b³值,那么我们实际上不仅具有a³和b³的所有可能值,而且还具有c³和d³的所有可能值。
  2. The sum of a³ + b³ is equal to the sum of c³ + d³

    a³+b³之和等于c³+d³之和
  3. If the sum of #2 above for a given pair of integers (a³, b³) matches the sum of another pair of integers (a³, b³), we have in essence found the c³ and d³ pair.

    如果对于给定的一对整数(a³,b³),上面#2的总和与另一对整数(a³,b³)的总和相匹配,则我们实际上找到了c³和d³对。

If we store every combination of the sum of a³ + b³ and the corresponding pair (a³, b³), any sum that has two pairs means we have found a³ + b³ = c³ + d³ where the first pair in the list can be considered (a³, b³) and the next (c³, d³).

如果我们存储a³+b³和与之对应的对(a³,b³)的每种组合,则任何具有两对的和都意味着我们找到a³+b³=c³+d³,可以将列表中的第一对视为( a³,b³)和下一个(c³,d³)。

For example, if we iterate through the combinations of a³ + b³, we will store the sum 1729 with the pair (1³, 12³). Continuing to iterate, we will see another sum of 1729 arise, but this time with the pair (9³, 10³). Because we have two different pairs both summing to 1729, we have found a Ramanujan Taxi that solves for a³ + b³ = c³ + d³.

例如,如果我们遍历a³+b³的组合,则将和1729与对(1³,12³)存储在一起。 继续进行迭代,我们将看到又产生了1729,但这次是货币对(9³,10³)。 因为我们有两个不同的货币对,总和为1729,所以我们找到了拉曼努(Ramanujan)出租车,可以解决a³+b³=c³+d³。

In the third version, we use a Hashmap to store the sum (key) and the corresponding list of pairs as a Sorted Set (value). If the list contains more than one pair, we’ve got a winner!

在第三个版本中,我们使用Hashmap将总和(键)和相应的对列表存储为排序集(值)。 如果列表中包含一对以上,我们就有赢家了!

This implementation has O(n²) performance since we only need two loops to calculate the combinations for a³ and b³. Very neat!

此实现具有O(n²)性能,因为我们只需要两个循环即可计算a³和b³的组合。 井井有条!

I suspect there is a forth optimization where we only need to calculate values of a³ and derive b³ from a³ (the ‘b’ loop is just an offset of the ‘a’ loop) with O(n) performance.

我怀疑有第四种优化方法,我们只需要计算a³的值并从a³得出b³(“ b”循环只是“ a”循环的偏移量),其性能为O(n)。

Also, another challenge is to re-write the implementations as a functional programming pattern. I’ll leave that for you to explore.

另外,另一个挑战是将实现重新编写为功能编程模式。 我将其留给您探索。

一部了不起的电影,一个了不起的人 (An Amazing Movie, an Amazing Man)

After watching The Man Who Knew Infinity, I was in awe of Ramanujan’s genius. By implementing his taxi algorithm — with its several performance optimizations — I got a glimpse of the beauty he saw in “No, Hardy, it’s a very interesting number!”

看完《知道无限的人》之后,我对拉马努詹的天才敬畏。 通过实施他的滑行算法及其多项性能优化,我瞥见了他在“不,哈代,这是一个非常有趣的数字!”中看到的美丽。

Ramanujan’s Taxi, at almost a century old, is still making new discoveries. Mathematicians at Emory University have found the number 1729 relates to elliptic curves and K3 surfaces — objects important today in string theory and quantum physics.

Ramanujan的的士已有近一个世纪的历史,仍在不断发现新发现。 埃默里大学的数学家们发现数字1729与椭圆曲线和K3曲面有关,而椭圆曲线和K3曲面是当今弦论和量子物理学中的重要对象。

I expect we have only scratched the surface of Ramanujan’s taxi cab number and the man’s amazing genius.

我希望我们只涉及Ramanujan的出租车号码和该男子的惊人天才的表面。

About the Author: Geoffrey Bourne is the CEO of RETIRETY — helping people in or near retirement find a better way to retire.

作者简介: Geoffrey BourneRETIRETY的首席执行官-帮助退休或接近退休的人们找到更好的退休方式。

谢谢阅读! (Thanks for reading!)

如果您喜欢这篇文章,请随时点击下面的鼓掌按钮? 帮助别人找到它! (If you enjoyed this article, feel free to hit that clap button below ? to help others find it!)

翻译自: https://www.freecodecamp.org/news/the-man-who-knew-infinity-coding-ramanujans-taxi-52e4c3696e53/

绝地求生米拉马延迟高

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值