使用 Euclid infinitude of prime proof 列出 N 以下的所有素数
Listing all primes below N using Euclid infinitude of prime proof
这里是问题:
Write a function that accepts a bound N, and finds the first N
primes as determined by the classical proof of the infinitude of the set
of primes. That is: begin with P = {2}; then form, m, the sum of
1 with the product over all elements of P. Place the smallest prime
factor of m into P and repeat.
这是我所做的:
def get_primes(n):
i = 2
prime_list = []
while i < n:
p = prod(prime_list)+1
r = min_symbolic(prime_divisors(p))
prime_list.append(r)
i = i+1
return prime_list
当我尝试 get_primes(10) 时,它给了我一堆错误,我在这里做错了什么?我刚开始学习python,谢谢。
编辑:抱歉不清楚,这是我遇到的错误
Error in lines 1-1
Traceback (most recent call last):
File "/projects/3fe731e3-2b4d-43b9-86a0-5efb4456f029/.sagemathcloud/sage_server.py", line 881, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "", line 6, in get_primes
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/sage/rings/arith.py", line 2538, in prime_divisors
return [p for p,_ in factor(n)]
TypeError: 'sage.symbolic.expression.Expression' object is not iterable
看起来你的 prod
可能在第一个事件中实际上不是可分解的。检查 prod([])+1
会发生什么。我在火车上,否则我会亲自为您检查一下。另外,我不确定为什么你不能只使用 min
,min_symbolic
实际上只适用于 min_symbolic(x,x^2)
之类的东西;你只是在寻找一些整数的最小值,所以内置 Python min
应该(?)工作正常。
kcrisman 是正确的,错误来自使用 min_symbolic。将其替换为 min 可消除错误,但仍需要对代码进行一些调整以提供前 n 个素数,如下所述:
def get_primes(n):
prime_list = [2]
i = 1
while i < n:
p = prod(prime_list) + 1
r = min(prime_divisors(p))
prime_list.append(r)
i = i + 1
return prime_list
这里是问题:
Write a function that accepts a bound N, and finds the first N primes as determined by the classical proof of the infinitude of the set of primes. That is: begin with P = {2}; then form, m, the sum of 1 with the product over all elements of P. Place the smallest prime factor of m into P and repeat.
这是我所做的:
def get_primes(n):
i = 2
prime_list = []
while i < n:
p = prod(prime_list)+1
r = min_symbolic(prime_divisors(p))
prime_list.append(r)
i = i+1
return prime_list
当我尝试 get_primes(10) 时,它给了我一堆错误,我在这里做错了什么?我刚开始学习python,谢谢。
编辑:抱歉不清楚,这是我遇到的错误
Error in lines 1-1
Traceback (most recent call last):
File "/projects/3fe731e3-2b4d-43b9-86a0-5efb4456f029/.sagemathcloud/sage_server.py", line 881, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "", line 6, in get_primes
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/sage/rings/arith.py", line 2538, in prime_divisors
return [p for p,_ in factor(n)]
TypeError: 'sage.symbolic.expression.Expression' object is not iterable
看起来你的 prod
可能在第一个事件中实际上不是可分解的。检查 prod([])+1
会发生什么。我在火车上,否则我会亲自为您检查一下。另外,我不确定为什么你不能只使用 min
,min_symbolic
实际上只适用于 min_symbolic(x,x^2)
之类的东西;你只是在寻找一些整数的最小值,所以内置 Python min
应该(?)工作正常。
kcrisman 是正确的,错误来自使用 min_symbolic。将其替换为 min 可消除错误,但仍需要对代码进行一些调整以提供前 n 个素数,如下所述:
def get_primes(n):
prime_list = [2]
i = 1
while i < n:
p = prod(prime_list) + 1
r = min(prime_divisors(p))
prime_list.append(r)
i = i + 1
return prime_list