Reputation: 52840
s=p=1;exec"if s%p*s%~-~p:print`p`+','+`p+2`\ns*=p*p;p+=2\n"*999
Upvotes: 7
Views: 1020
Reputation: 1797
It calculates and prints the sets of twin prime numbers.
3,5
5,7
11,13
17,19
29,31
41,43
59,61
71,73
101,103
107,109
137,139
.....
Very cool :)
Upvotes: 7
Reputation: 52840
The code is iterative.
s=p=1
, initializationexec"f(...)"*999
is the same as for i in range(999):f(...)":
s%p
is a modulo p*s
is a multiplication (x,y)
, binary operation~-~
explained here.\n
means line break,\ns
means a line break and s
is a part of the declaration s*=p*p;
p+=2
means the assignment p=p+2
Hopefully, other people can fill the gaps. For futher investigation, what is its recursive equation?
Upvotes: 3
Reputation: 26098
Here is an unraveling of the basic idea.
# p = 1; s = p
s=p=1
#exec"if s%p*s%~-~p:print`p`+','+`p+2`\ns*=p*p;p+=2\n"*999
for i in range(999):
# s%p = remainder of s/p
# ~p = 1s complement of p
if s%p*s%~-~p:
# `p` = repr(p)
print`p`+','+`p+2`
# s = s*p*p
s*=p*p
# p = p+2
p+=2
Upvotes: 12