Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this Python one-liner mean?

Tags:

python

syntax

s=p=1;exec"if s%p*s%~-~p:print`p`+','+`p+2`\ns*=p*p;p+=2\n"*999

Source.

like image 571
hhh Avatar asked Dec 25 '10 05:12

hhh


2 Answers

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
like image 100
kevpie Avatar answered Oct 08 '22 16:10

kevpie


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 :)

like image 32
esmit Avatar answered Oct 08 '22 14:10

esmit