[英]Better algorithm on prime numbers
I'm working on a program which will found the n
th. 我正在开发第
n
个程序。 prime number. 质数。 For example, By listing the first six prime numbers:
2, 3, 5, 7, 11
and 13
, we can see that the 6th prime is 13
. 例如,通过列出前六个质数:
2, 3, 5, 7, 11
和13
,我们可以看到第6个质数是13
。 I'm trying to making an algorithm, like, if I want to see 50th prime, I will add 1
to ends of the range()
function. 我正在尝试制定一种算法,例如,如果我想看第50个素数,我将在
range()
函数的末尾加1
。 I'm using this algorithm to find primes at the moment; 我现在正在使用此算法查找素数;
cnt = 1
print (2)
for x in range(3,40,2):
div = False
for y in range(2,round(x**0.5)+1):
if x%y == 0:
div = True
if div == False:
print (x)
cnt += 1
print ("\nThere is {} prime numbers.".format(cnt))
You see that, I put 40
. 你看,我放
40
。 But I want to put n
there, so for example, untill reaching the 50th prime, add +1 to n
. 但是我想把
n
放在那里,例如,直到达到第50个素数,将+1加到n
。 But it's not going to like that, if I tried something like; 但是,如果我尝试过类似的事情,那是不会的。
cnt = 1
n = 40 #example
while cnt<50:
for x in range(3,n,2):
#codes
if div == False:
n += 1
I thought when the program finds a prime, it will add +1 to n
and while
loop will process untill find the 50th prime. 我以为程序找到素数时,它将为
n
加+1,而while
循环将处理直到找到第50个素数。 But it didn't, primes are wrong if I use this one also, nothing relevant what I want to do. 但事实并非如此,如果我也使用素数,则素数是错误的,与我想做的事情无关。
How to make this algorithm, obviously changing the last element of range()
function does not working. 如何制作此算法,显然更改
range()
函数的最后一个元素无效。
Is there better/elegant algorithm/way? 有更好/更优雅的算法/方法吗? If I want to find
200.000th
prime, I need faster codes. 如果我想找到第
200.000th
素数,则需要更快的代码。
Edit: I was working with lists first but, I got MemoryError all the time when working with big numbers. 编辑:我首先使用列表,但是在处理大数时一直出现MemoryError。 So I pass that and using a variable that counting how much primes are there
cnt
. 所以我将其传递给我,并使用一个变量来计算
cnt
有多少个素数。
Here is a much faster version 这是一个更快的版本
primes = []
primecount = 0
candidate = 2
while primecount<50:
is_prime = True
for prime in primes:
if candidate%prime == 0:
is_prime = False
break
elif candidate < prime**2:
break
if is_prime:
primes.append(candidate)
primecount += 1
candidate += 1
print primes[-1]
note small edit adding the candidate<prime**2
test that OP included but I neglected initially. 请注意,进行少量编辑,添加OP包含的
candidate<prime**2
测试,但最初我忽略了。
Your code is going to be very slow for several reasons. 由于以下几个原因,您的代码将会非常缓慢。 If 2 divides a number you know it's not prime, but you're still checking whether 3 or 4 or 5... divides it.
如果2除以一个数字,您就知道它不是质数,但您仍在检查3或4或5 ...是否将其除。 So you can break out as soon as you know it's not prime.
因此,只要您知道它不是主要的,就可以爆发。 Another major issue is that if 2 does not divide a number, there's no reason to check if 4 divides it as well.
另一个主要问题是,如果2不除数,则没有理由检查4是否除数。 So you can restrict your attention to just checking if the primes coming before it divide it.
因此,您可以将注意力集中在仅检查素数是否将其分割之前。
In terms of run time: 在运行时间方面:
First off, for backwards compatibility with python 2, I added an int()
around your rounding of root x. 首先,为了与python 2向后兼容,我在根x的四舍五入周围添加了一个
int()
。
From what I understand of your question, you are looking for something like this: 据我对您的问题的了解,您正在寻找这样的东西:
cnt = 1
maximum=50 #Specify the number you don't want the primes to exceed
print (2)
n=20 #highest number
while cnt<maximum: #
for x in range(3,n,2): #using "n" as you hoped
div = False
for y in range(2,int(round(x**0.5)+1)):
if x%y == 0:
div = True
if div == False:
if x<maximum: #we only want to print those up to the maximum
print str(x)
else: #if it is greater than the maximum, break the for loop
break
cnt += 1
break #break the while loop too.
print ("\nThere are {} prime numbers.".format(cnt))
This gave me the correct primes. 这给了我正确的素数。 As for better/more elegant solutions.
至于更好/更优雅的解决方案。 If by better you want speed, use a library that has an optimized, or look at this for an example of a fast program.
如果通过更好地你想要的速度,使用具有优化的图书馆,还是看这对于快速程序的例子。 Elegance is subjective, so I will leave that to the rest of the internet.
优雅是主观的,因此我将其留给互联网的其余部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.