-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0004.py
More file actions
78 lines (67 loc) · 2.47 KB
/
Problem0004.py
File metadata and controls
78 lines (67 loc) · 2.47 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
Enonce = """
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 = 91 x 99.
Find the largest palindrome made from the product of two 3-digit numbers.
"""
# Iterator style
class Product:
def __init__(self, size=2):
if size >= 2:
self.size = size
else:
self.size = 2
def __iter__(self):
self.factors = [int(self.size*'9'), int(self.size*'9')]
self.end = [int((self.size-1)*'9'), int((self.size-1)*'9')]
return self
def __next__(self):
if self.factors == self.end :
raise StopIteration
product, factor1, factor2 = self.factors[0]*self.factors[1], self.factors[0], self.factors[1]
# next values
if self.factors[0] == 0 :
self.factors[0] = int(self.size*'9')
self.factors[1] = self.factors[1] - 1
else:
self.factors[0] = self.factors[0] - 1
return product, factor1, factor2
def isPalindrome(number):
number_str = str(number)
#print(f"len(number_str)={len(number_str)} ; number_str={number_str} ; number_str[-1::-1]={number_str[-1::-1]}")
return (len(number_str) >= 1) and (number_str == number_str[-1::-1])
def main():
print(40*"=")
print(Enonce)
print(40*"-")
import time
#start = time.perf_counter()
#Solutions = {}
#Solution = []
#digits = 3 #2
#for product, factor1, factor2 in Product(digits):
#if isPalindrome(product):
##print(f"{factor1} x {factor2} = {product} is a palindrome)")
#Solutions[product] = [product, factor1, factor2]
#Solution.append(product)
#product, factor1, factor2 = Solutions[max(Solution)]
#Solution = f"{factor1} x {factor2} = {product} is the highest palindrome with {digits} digits)"
#end = time.perf_counter()
#print(f"{Solution} en {end-start} secondes")
start = time.perf_counter()
Solution = [0, 0, 0]
digits = 3 #2
for product, factor1, factor2 in Product(digits):
if (product > Solution[0]) and isPalindrome(product):
Solution = [product, factor1, factor2]
product, factor1, factor2 = Solution
Solution = f"{factor1} x {factor2} = {product} is the highest palindrome with {digits} digits)"
end = time.perf_counter()
print(f"{Solution} en {end-start} secondes")
print(f"{Solution}")
print(40*"-")
print("Solution = {}".format(Solution))
print(40*"=")
if __name__ == "__main__":
# execute only if run as a script
main()