-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP037.py
More file actions
82 lines (72 loc) · 1.99 KB
/
P037.py
File metadata and controls
82 lines (72 loc) · 1.99 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
#==============================================================================
# The number 3797 has an interesting property. Being prime itself,
# it is possible to continuously remove digits from left to right,
# and remain prime at each stage: 3797, 797, 97, and 7. Similarly
# we can work from right to left: 3797, 379, 37, and 3.
#
# Find the sum of the only eleven primes that are both truncatable
# from left to right and right to left.
#
# NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
#==============================================================================
import math
import numpy as np
def isPrime(n):
'''check if integer n is a prime'''
# make sure n is a positive integer
n = abs(int(n))
# 0 and 1 are not primes
if n < 2:
return False
# 2 is the only even prime number
if n == 2:
return True
# all other even numbers are not primes
if not n & 1:
return False
# range starts with 3 and only needs to go up the squareroot of n
# for all odd numbers
for x in range(3, int(math.sqrt(n))+1, 2):
if n % x == 0:
return False
return True
PMax = 1000001
PList = np.ones(PMax)
PList[1] = 0
ii = 2
while (ii*ii < PMax):
if PList[ii]:
# print 'Testing', ii
jj = 2*ii
while jj < PMax:
PList[jj] = 0
jj += ii
ii += 1
Primes = []
for ii in range(1,PMax):
if PList[ii]:
Primes.append(ii)
def isTrunc(Prime):
sPrime = str(Prime)
nlen = len(sPrime)
try:
for nn in range(1,nlen):
# print Prime, 'testing', sPrime[nn:], sPrime[:-nn]
Primes.index(int(sPrime[nn:]))
Primes.index(int(sPrime[:-nn]))
except:
return False
return True
idx = 5
ntot = 0
nsum = 0
print 'Finding Trunc'
while ntot < 11:
if isTrunc(Primes[idx]):
print '***WINNER***', Primes[idx]
ntot += 1
nsum += Primes[idx]
idx += 1
print nsum
# 748317