-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path037_truncatable_primes.py
64 lines (47 loc) · 1.3 KB
/
037_truncatable_primes.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
59
60
61
62
63
64
"""
Task:
"""
import time
def main():
primes = set(all_primes(1000000))
wanted_nums = []
for prime in primes:
prime_str = str(prime)
is_truncatable = True
for i in range(1, len(prime_str)):
if int(prime_str[i:]) not in primes:
is_truncatable = False
if int(prime_str[:len(prime_str)-i]) not in primes:
is_truncatable = False
if is_truncatable:
wanted_nums.append(prime)
wanted_nums.remove(2)
wanted_nums.remove(3)
wanted_nums.remove(5)
wanted_nums.remove(7)
print(wanted_nums)
print("len", len(wanted_nums))
print(sum(wanted_nums))
def all_primes(limit):
# list containing for every number whether it has been marked already
numbers = {}
for x in range(3, limit, 2):
numbers[x] = False
primes = [2, 3]
p = 3
while p < limit:
for i in range(p, limit, p):
numbers[i] = True
for i in range(p, limit, 2):
if not numbers[i]:
p = i
numbers[i] = True
primes.append(i)
break
else:
p += 1
return primes
if __name__ == '__main__':
start_time = time.time()
main()
print("time:", time.time() - start_time)