-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0655_3_FAIL.py
More file actions
63 lines (49 loc) · 1.81 KB
/
Problem0655_3_FAIL.py
File metadata and controls
63 lines (49 loc) · 1.81 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
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 ?
"""
class Palindrome:
"""Increment by start value and then check if it is a palindrome"""
def __init__(self, max, start=1):
self.max = max
self.start = start
def __iter__(self):
self.value = self.start # Real value of palindrome
return self
def __next__(self):
self.value = self.value + self.start
while(not self.isPalindrome(self.value)):
self.value = self.value + self.start
if self.value >= self.max :
raise StopIteration
return self.value
def isPalindrome(self, 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
digits = 10 #8 #32 #5
divider = 1095 #109 #10_000_019 #109
# Palindrome(pow(10, 12), {1095}): 9.653235062 secondes
# Palindrome_3(pow(10, 12), {1095}): 1087.373263688 secondes
print(f"Palindrome(pow(10, {digits}), {divider}):")
start = time.perf_counter()
Solution = 0
for i in Palindrome(pow(10, digits), divider):
#print(i)
Solution = Solution + 1
end = time.perf_counter()
print(f"{Solution} en {round(end-start, 3)} 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()