-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLychrel.py
95 lines (79 loc) · 2.12 KB
/
Lychrel.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
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
import time
from decimal import Decimal
def pal(x) : # <int>
""" Return the palindrome of a int number x. """
x = int(x)
num = x
m = 0
while num > 0 :
m = m*10 + num%10
m = int(m)
num = num // 10
num = int(num)
return m # <int>
def ispal(x) : # <int>
"""Palindrome detector.
Return True if it's a palindrome, False otherwise."""
x = int(x)
num = x
m = 0
while num > 0 :
m = m*10 + num%10
m = int(m)
num = num // 10
num = int(num)
if m == x :
return True # <bool>
else :
return False # <bool>
def lychrel(x, detailed = False) : # <int>
""" Function carrying out the sequences of operations
leading to the palindrome.
- The intermediate stages of the operation are inserted in a
list until the palindrome is obtained.
- By default, only the result of intermediate operations is
added to the list, but a more detailed display of the
calculations is possible by setting 'detailed = True',
in this case, each addition and its result appear in tulps.
- A duration in seconds is allocated to the calculation,
beyond this allotted time the process stops, meaning that
the number inserted is potentially a Lychrel number"""
x = int(x)
nbr = x # x = initial value, nbr = value to handle
wd = 5 # Time allowed for calculation (sec)
L = []
start_time = time.time()
while True :
res = nbr + pal(nbr)
# Time control (potential Lychrel number)
exe_time = time.time()
if exe_time >= start_time + wd :
res = Decimal(res) #to print scientific notation
stop = []
stop.append("ITER. STOP")
stop.append(wd)
stop.append("{:.2e}".format(res))
stop.append(x)
L = []
L.append(stop)
break
# It's a palindrome
if ispal(res) == True :
if detailed == True:
L.append([nbr, pal(nbr), res])
else:
L.append(res)
break
# NOT a palindrome
elif ispal(res) == False :
if detailed == True:
L.append([nbr, pal(nbr), res])
else:
L.append(res)
nbr = res
continue
return L # <list>
if __name__ == "__main__" :
print("192 (default output) : ", lychrel(192))
print("192 (detailed output) : ", lychrel(192, detailed=True))
print("196 :", lychrel(196))