Today saw "Machine Ji", to explore the topic of artificial intelligence films, watercress rating 7.5, or pretty good movie. A python code appeared at 1:09:29 in the movie, and a closer look at the Python code, found to be the Sieve method for prime numbers, was written very concisely. Put on a film first:
The code in the movie is a little blurry, and I'm re-playing it the following way
| 123456789101112131415161718192021222324252627 |
#coding:utf8 importsys defsieve(n): #compute primes using sieve eratosthenes x = [1] *n x[1] =0 fori inrange(2,n/2): j =2*i whilej < n: x[j] =0 j =j +i returnx def prime(n,x): #Find nth prime i =1 j =1 whilej <= n: ifx[i] ==1: j =j +1 i =i +1 returni-1x =sieve(10000) code =[1206,301,384,5] key =[1,1,2,2] sys.stdout.write("".join(chr(i) fori in[73,83,66,78,32,61,22])) fori inrange(0,4): sys.stdout.write(str(prime(code[i],x)-key[i])) |
The final print of the code below this very strange thing, visual inspection is a book of ISBN, on the Watercress check, is embodiment and the Inner life, is about thinking, the content of consciousness, and the theme of this film is closely related.
| 1 |
ISBN =9780199226559[Finished in0.1s] |
The focus is on the first two functions to achieve the Sieve method to find prime numbers. First of all, what is the Sieve method, the Sieve method is the ancient Greek Eratosthenes invented an algorithm for detecting prime numbers. The idea of sieve method is very simple, can be described by the following motion diagram. Given a range, first use 2 to sift, all 2 of the multiples are sieved out, and then use 3 sieve, with 5 sieve, and constantly repeated ...
Look at the code again.
| 12345678910111213141516171819 |
def sieve(n): //对n以内的数进行筛选,返回一个长度为n的布尔数组 #compute primes using sieve eratosthenes x =[1] * n //定义长度为n的布尔数组(实际上电影里用1和0来表示true和false了) x[1] =0//1既不是素数也不是合数,设为0 fori inrange(2,n/2)://i从2开始一直到n/2 j =2*i //j从2倍i开始 whilej < n: x[j] =0//把所有i的倍数筛除 j =j + i //下一个i的倍数 returnx //返回数组 defprime(n,x): //求第n个素数,只需要在筛选好的布尔数组中找第n个标记为1的数就可以了 #Find nth prime i =1//初始化为1 j =1 whilej <=n: //在布尔数组中寻找第n个标记为1的数 ifx[i] ==1: j =j +1 i =i +1 return i-1//前面循环中i多加了一次,返回时需要减1 |
It can be seen that the time complexity of using sieve method to find the nth prime number is O (n), the disadvantage is to obtain the result of the filter in advance, increase the spatial complexity, and the filter result can be represented by bit to save space.
There is also a question of how to determine the approximate range of nth prime numbers in order to determine the length of the Boolean array to filter the results when seeking nth prime numbers. According to the prime number theorem, it can be used to estimate the number of elements in a range, can be described by the formula X/LN (x), ln represents the natural logarithm, assuming to estimate how many prime numbers within 10000, substituting the formula 10000/LN (10000) results are 1085.73, Using the above sieve method to obtain 10000 of the number of the mass number of 1229, you can see the estimated value is slightly smaller than the actual value, the larger the estimated range, the lower the error between the estimated value and the actual value. In practice, estimates can be calculated by formula and then expanded by a certain percentage.
http://www.qytang.com/cn/list/28/611.htm
http://www.qytang.com/cn/list/28/610.htm
http://www.qytang.com/cn/list/28/595.htm
http://www.qytang.com/cn/list/28/583.htm
http://www.qytang.com/cn/list/28/582.htm
http://www.qytang.com/cn/list/28/576.htm
http://www.qytang.com/cn/list/28/523.htm
http://www.qytang.com/cn/list/28/499.htm
http://www.qytang.com/cn/list/28/488.htm
http://www.qytang.com/cn/list/28/466.htm
http://www.qytang.com/cn/list/28/463.htm
http://www.qytang.com/cn/list/28/458.htm
http://www.qytang.com/cn/list/28/455.htm
http://www.qytang.com/cn/list/28/447.htm
http://www.qytang.com/cn/list/28/446.htm
http://www.qytang.com/cn/list/28/445.htm
Http://www.qytang.com
The code in the movie "Mechanical Ji": Sieve method for prime numbers