这不是作业,我只是好奇.
INFINITE是这里的关键词.
我希望在primes()中使用它作为p.我相信这是Haskell中的内置函数.
所以,答案不能像"Just do a Sieve"那样天真.
首先,您不知道将消耗多少连续素数.好吧,假设你可以一次编制100个.您是否会使用相同的Sieve方法以及素数公式的频率?
我更喜欢非并发方法.
感谢您阅读(和写作;))!
#!/usr/bin/python
import sys,math
n = input("enter a number to find the factors : ")
j,flag,b= 0l,False,0l
for b in xrange(1,n+1):
a = n + (b*b)
j = long(math.sqrt(a))
if a == j*j:
flag = True
break
if flag:
c = j+b
d = j-b
print "the first factor is : ",c ," and the second factor is : ",d
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,它会为不同的输入抛出不同类型的错误.
以下是一种输入
linux@terminal:~$ ./fermat.py
enter a number to find the factors : 544564564545456
Traceback (most recent call last):
File "./fermat.py", line …Run Code Online (Sandbox Code Playgroud) 有关如何让这个程序工作n = 1万亿(除了升级/购买新计算机)的任何建议?
错误如下:构建后,正在执行的程序(命令行样式输出窗口弹出)然后快速关闭,我得到以下错误"ProjectPrimes.exe已停止工作(Windows正在寻找解决方案这个问题."我怀疑这与内存问题有关,因为我第一次遇到n = 2000万,但那是在我选择malloc /释放'筛'阵列之前(即我的'筛'阵列是大阵列)尺寸nx 1,每个元素由1或0组成.
该程序需要大约35秒才能完成前3亿个整数(16,252,325个素数),所以没关系,但没什么了不起的.正如我所提到的,目标是能够产生低于1万亿的质数,所以我还有很长的路要走......
如果相关,这是我的机器规格(如果在这台机器上目标恰好是不合理的):2.40ghz i5,4GB RAM,64位Windows 7.
方法概述,对于那些不熟悉的人:我们使用Sienda of Sundaram方法.在没有进入证明的情况下,我们首先使用筛选函数消除整数"n"以下的所有奇数非素数:[2*(i + j + 2*i*j)+1 | i < - [1..n/2],j < - [i..an优化上限]].然后我们交掉偶数(当然不包括两个).这让我们留下了素数.
为什么prime函数返回(指向包含数组的指针)n下面的完整素数集?那么,目标是能够识别(i)n以下的素数以及(ii)列出n以下的素数.这也是为什么我选择传递一个指针,用于将n下面的素数计数作为参数.
这是不那么令人兴奋的"主要"功能:
int main() {
long ceiling = 300*1000*1000;
long *numPrimes;
long *primes;
primes = primesToSS(ceiling+1, numPrimes);
printf("\n\nThere are %d primes below %d.\n\n",*numPrimes,ceiling);
free(primes);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是肉:
//n represents the ceiling, i.e., the integer below which we will generate primes
//cnt* is a pointer which will point the number of primes …Run Code Online (Sandbox Code Playgroud) 我需要在Python中使用生成器生成素数.这是我的代码:
def genPrimes():
yield 2
x=2
while True:
x+=1
for p in genPrimes():
if (x%p)==0:
break
else:
yield x
Run Code Online (Sandbox Code Playgroud)
我有一个RuntimeError:当我运行它时,在第二个prime.next()之后超出了最大递归深度.