Python 3中,124个 123字节
i=3
q=[2]
while 1:
p=1
for x in range(2,round(i**.5)+1):p=min(p,i%x)
if p:
q+=[i];s=(i-1)/2
if s in q:print(s)
i+=2
它是如何工作的?
i=3 # Start at 3
q=[2] # Create list with first prime (2), to be list of primes.
while 1: # Loop forever
p=1 # Set p to 1 (true)
for x in range(2,round(i**0.5)+1): # Loop from 2 to the number's square root. x is the loop value
p=min(p,i%x) # Set p to the min of itself and the modulo of
# the number being tested and loop value (x).
# If p is 0 at the end, a modulo was 0, so it isn't prime.
if p: # Check if p is 0
q+=[i] # Add the current number (we know is prime) to list of primes (q)
s=(i-1)/2 # Generate s, the number that you would double and add 1 to make a prime.
if s in q:print(s) # If (i-1)/2 is a prime (in the list), then that prime satifies
# the condition 2p+1 is prime because i is 2p+1, and i is prime
i+=2 # Increment by 2 (no even numbers are prime, except 2)
在这里在线尝试。
我的计算机说,它已经生成了低于2 ^ 32的所有Sophie Germain素数的0.023283%。
完成后,如果有足够的行,我将其发布到pastebin上。您可以使用它来检查所有内容。