-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path37.py
69 lines (61 loc) · 1.28 KB
/
37.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
65
66
67
68
69
import resource
import math
import time
t1 = time.monotonic()
def isPrime (num):
if (num%2 == 0):
return False
else:
ub = math.ceil(num**(1/2))
j = 3
while (j<=ub):
if (num%j == 0):
return False
j+=2
return True
def rightTrucatedPrimes (num):
if (num in [2,3,5,7]):
return True
elif(isPrime(num)):
return(rightTrucatedPrimes(num//10))
else:
return False
def leftTruncatedPrimes (num,exp):
if (num in [2,3,5,7]):
return True
elif(isPrime(num)):
return leftTruncatedPrimes(num%(10**exp), exp-1)
else:
return False
def getExp (num):
exp = math.floor(math.log(num,10)) + 1
return exp
twoSidedPrimes = 0
num = 11
total = 0
while (twoSidedPrimes <=10):
if (rightTrucatedPrimes(num)):
if(leftTruncatedPrimes(num,getExp(num))):
twoSidedPrimes += 1
total += num
print(num)
num += 2
print('Sum is',total)
print(time.monotonic() - t1,'s')
print(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1000 , 'KB')
'''
#Failed recursive approach
array = list(range(1,10))
def RemoveFromLtoRTruncPrimes (num):
i = 0
if (isPrime(num) and counter <5):
num = num*10 + array[i]
i+=1
return (RemoveFromLtoRTruncPrimes(num))
elif(num>=10):
num //= 10
return (RemoveFromLtoRTruncPrimes (num))
else:
counter = 0
print(RemoveFromLtoRTruncPrimes(2))
'''