Having a Google Chrome Cast at home increased my longform consumption on Youtube dramatically. For instance, I discovered this talk about the great mathematician Euler by William Dunham:
You can check it by running following Python code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def isPrime(n): | |
"""Returns True if n is prime http://stackoverflow.com/a/1801446/2987382""" | |
if n == 2 or n==3: return True | |
if n < 2 or n % 2 == 0: return False | |
if n % 3 == 0: return False | |
i = 5 | |
w = 2 | |
while i * i <= n: | |
if n % i == 0: | |
return False | |
i += w | |
w = 6 - w | |
return True | |
# Generate primes using Euler's polynomial x**2+x+41 | |
# Optimized to avoid multiplications | |
primes = [] | |
px = 41 # px contains the value of p(x) = x^2+x+41 | |
x = 0 | |
while isPrime(px): | |
primes.append(px) | |
px += 2*x+2 | |
x += 1 | |
print len(primes), " primes generated by Eulers polynomial x**2+x+41" | |
print primes | |
# Output from 'python euler.py': | |
# | |
# 40 primes generated by Eulers polynomial x**2+x+41 | |
# [41, 43, 47, 53, 61, 71, 83, 97, 113, 131, 151, 173, 197, 223, 251, 281, | |
# 313, 347, 383, 421, 461, 503, 547, 593, 641, 691, 743, 797, 853, 911, 971, | |
# 1033, 1097, 1163, 1231, 1301, 1373, 1447, 1523, 1601] |
The solution to Euler problem 27 is also interesting. A second degree polynomial generates 71 primes (but they are not consecutive). The polynomial x^2 - 61x + 971 generates 71 primes for x \in [0,70]. This is the longest solution when the absolute values of the coefficients are restricted to thousand. If we drop that restriction, an even stronger polynomial is found : x^2 -79 x + 1601 generates 80 primes. Remark that the generated primes are not unique. For example the numbers 1601, 41, 197, 797, 1373, 1523 are generated twice. From the 80 primes generated, 40 are unique. I think Euler would not have been impressed.
No comments:
Post a Comment