-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimePermutations-49
60 lines (50 loc) · 1.4 KB
/
PrimePermutations-49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
# isprime is from http://pythonism.wordpress.com/2008/05/04/looking-at-prime-numbers-in-python/
def isprime(n):
n*=1.0
if n%2==0 and n!=2 or n%3==0 and n!=3:
return False
for b in range(1,int((n**0.5+1)/6.0+1)):
if n%(6*b-1)==0:
return False
if n %(6*b+1)==0:
return False
return True
"""
prime permutations problem
Euler problem 49
By: A.O.
"""
import sys
import os
import string
import itertools
from heapq import merge
def main(argv):
try:
primes = list(merge([n for n in range(1001, 10000, 6) if isprime(n)],\
[n for n in range(1003, 10000, 6) if isprime(n)]))
#print primes
for p in primes:
#permutations = [int(''.join(x)) for x in itertools.permutations(str(p),4)]
permutations = []
for x in itertools.permutations(str(p),4):
number = int(''.join(x))
if (number not in permutations):
permutations.append(number)
for perm in permutations:
if (perm > p and perm in primes):
diff = perm - p
last = perm + diff
if (last in primes and last in permutations):
print "Found %d,%d,%d" % (p, perm, last)
if (p != 1487):
return None
else:
continue
else:
continue
except "":
return None
if __name__ == "__main__":
main(sys.argv)