I'm following 3Blue1Brown's online Youtube video found here: Youtube:3Blue1Brown and I'm using his implementation of finding prime numbers. You can see the program and the first set of his outputs @1:10 in the video.
现在,他正在使用Python v.3.7.0,而我正在使用Python v.3.7.4。我已经安装了Numpy,并且解释器已集成到Window的命令提示符中。我不是在创建实际的Python文件,而是直接在Python的解释器中运行代码。
这是程序和结果的整个命令提示符输出的副本...
C:\Users\skilz99>python
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import math
>>>
>>> def get_primes(n_min, n_max):
... result = []
... for x in range(max(n_min, 2), n_max):
... has_factor = False
... for p in range(2, int(np.sqrt(x)) + 1):
... if x % p == 0:
... has_factor = True
... break
... if not has_factor:
... result.append(x)
... return result
...
>>> get_primes(0,50)
[5, 7, 9, 11, 11, 13, 13, 15, 17, 17, 17, 19, 19, 19, 21, 23, 23, 23, 25, 25, 25
, 27, 29, 29, 29, 29, 31, 31, 31, 31, 33, 35, 35, 35, 37, 37, 37, 37, 37, 39, 41
, 41, 41, 41, 41, 43, 43, 43, 43, 43, 45, 47, 47, 47, 47, 47, 49, 49, 49, 49, 49
]
>>>
为什么我得到的结果与他完全不同?我不知道这是因为Python版本不同还是他使用的Numpy版本与我的版本不同。但是我想认为像这样的简单程序应该产生相同的结果。
我相信您要编写的代码是:
The problem was the second if statement where it is decided if
x
should or not be appended to theresult
list.