开发者

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.

  1. s=p=1, initialization
  2. exec"f(...)"*999 is the same as for i in range(999):f(...)":
  3. s%p is a modulo
  4. p*s is a multiplication (x,y), binary operation
  5. ~-~ explained here.
  6. \n means line break,
  7. \ns means a line break and s is a part of the declaration s*=p*p;
  8. p+=2 means the assignment p=p+2

Hopefully, other people can fill the gaps. For futher investigation, what is its recursive equation?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜