What does this Python one-liner mean?
s=p=1;e开发者_如何学Goxec"if s%p*s%~-~p:print`p`+','+`p+2`\ns*=p*p;p+=2\n"*999
Source.
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
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 :)
The code is iterative.
s=p=1
, initializationexec"f(...)"*999
is the same asfor i in range(999):f(...)":
s%p
is a modulop*s
is a multiplication(x,y)
, binary operation~-~
explained here.\n
means line break,\ns
means a line break ands
is a part of the declarations*=p*p;
p+=2
means the assignmentp=p+2
Hopefully, other people can fill the gaps. For futher investigation, what is its recursive equation?
精彩评论