-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_004_00.py
More file actions
107 lines (82 loc) · 2.69 KB
/
problem_004_00.py
File metadata and controls
107 lines (82 loc) · 2.69 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
##Largest palindrome product
##Problem 4
##A palindromic number reads the same both ways. The largest palindrome
##made from the product of two 2-digit numbers is 9009 = 91x99.
##
##Find the largest palindrome made from the product of two 3-digit numbers.
import numpy as np
import time
def primesfrom2to(n):
""" Input n>=6, Returns a array of primes, 2 <= p < n """
sieve = numpy.ones(n/3 + (n%6==2), dtype=numpy.bool)
for i in range(1,int((n**0.5)/3+1)):
if sieve[i]:
k=3*i+1|1
sieve[ k*k/3 ::2*k] = False
sieve[k*(k-2*(i&1)+4)/3::2*k] = False
return numpy.r_[2,3,((3*numpy.nonzero(sieve)[0][1:]+1)|1)]
def calculatedivisors(maxlimit):
npdivisors = []
k = 0
while k == 0:
for i in range(2, int(maxlimit/2)):
if maxlimit % i == 0:
npd = np.ones(len(npdivisors), dtype=bool)
m = 0
try:
for j in npdivisors:
if i % j == 0:
npd[m] = False
break
m += 1
if npd.all():
npdivisors.append(i)
product = 1
for element in npdivisors:
product = element * product
if product == maxlimit:
k = 1
return npdivisors
except:
npdivisors.append(i)
print(npdivisors)
return npdivisors
def findpalind(Nmax):
list1 = []
for i in range(1, Nmax):
list1.append(i)
list1.reverse()
list2 = list1[:]
listpalind = set()
m = 0
while m == 0:
for i in list1:
for j in list2:
k = i * j
if str(k) == str(k)[::-1]:
## print("str(k) == str(k)[-1:1]", str(k), str(k)[::-1])
listpalind.add(k)
## print("appending ", k)
m = 1
## print(listpalind)
return listpalind
def isPal(s):
if len(s) <= 1:
return True
else:
return s == s[::-1]
def checkpals(Nmax):
val = 0
for i in range(Nmax,1,-1):
for j in range(Nmax,1,-1):
if isPal(str(i*j)) and i*j > val:
val = i*j
return val
def main():
start = time.time()
## palind = findpalind(1000)
## print(max(palind))
print(checkpals(1000))
end = time.time()
print(end -start)
main()