-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem0655_2_FAIL.py
69 lines (56 loc) · 1.88 KB
/
Problem0655_2_FAIL.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
Enonce = """
Divisible Palindromes
Problem 655
The numbers 545, 5995 and 15151 are the three smallest palindromes divisible by 109. There are nine palindromes less than 100000 which are divisible by 109.
How many palindromes less than 1032 are divisible by 10000019 ?
"""
# Iterator style
class Palindrome:
"""Construct palindromes"""
def __init__(self, max, start=1):
self.max = max
self.start = int( str(start)[:int(round(len(str(start))/2 + 0.1))] )
self.start = round(self.start/50)
def __iter__(self):
self.odd = True # number of digits is odd or not (even)
self.base = self.start # Half side of palindrome
self.value = 1 # Real value of palindrome
return self
def __next__(self):
string = str(self.base)
if self.odd:
self.value = int(string[:] + string[-2::-1]) # as 'abcba'
# End if lower value is to high
if self.value >= self.max:
raise StopIteration
else:
self.value = int(string[:] + string[-1::-1]) # as 'abccba'
self.base = self.base + self.start
self.odd = not self.odd
# value to high, return next odd value
if self.value >= self.max:
self.__next__()
return self.value
def main():
print(40*"=")
print(Enonce)
print(40*"-")
import time
digits = 10 #32 #5
divider = 1095 #10_000_019 #109
print(f"Palindrome(pow(10, {digits}), {divider}):")
start = time.perf_counter()
Solution = 0
for i in Palindrome(pow(10, digits), divider):
if (i % divider) == 0:
#print(i)
Solution = Solution + 1
end = time.perf_counter()
print(f"{Solution} en {end-start} secondes")
#print(f"{Solution}")
print(40*"-")
print(f"There are {Solution} palindromes less than 10^{digits} and divisible by {divider}")
print(40*"=")
if __name__ == "__main__":
# execute only if run as a script
main()