-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem37.py
58 lines (41 loc) · 1.38 KB
/
problem37.py
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
#!/usr/bin/python
'''
Project Euler
Problem 37
'''
from util import PrimeSieve
__directions = ['LEFT','RIGHT']
def permutations_removing_end_digit(direction, num):
''' Generate list of numbers from seeding num where digits are
trimmed from the desired end. '''
assert direction in __directions, 'Invalid direction. Use CAPS.'
permutations = [num]
str_num = str(num)
for i in range(1,len(str_num)):
if direction is 'LEFT':
permutations.append(str_num[i:])
else:
permutations.append(str_num[:i])
return permutations
def is_two_way_truncatable_prime(sieve, num):
''' Given a number. Test all trimmed permutations for primality. '''
permutations = permutations_removing_end_digit('LEFT', num) + \
permutations_removing_end_digit('RIGHT', num)
permutations = set(permutations)
for permute in permutations:
if sieve.is_prime(int(permute)) is False:
return False
return True
if __name__ == '__main__':
''' Generate sieve and check for two way truncatable primes.
Expect 11 values. '''
trial_limit = 1000000
sieve = PrimeSieve(trial_limit)
sieve.generate()
answer = []
for i in range(8, trial_limit-1):
if is_two_way_truncatable_prime(sieve, i) is True:
answer.append(i)
print answer
print sum(answer)
print len(answer)