-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHappyNumbers.py
More file actions
52 lines (46 loc) · 1.11 KB
/
Copy pathHappyNumbers.py
File metadata and controls
52 lines (46 loc) · 1.11 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
from multiprocessing import Pool
import time
st = set()
sf = set()
def sqre(n):
s = 0
while n:
n, d = divmod(n, 10)
s += d**2
return s
def ishappy(n):
global st
global sf
s = []
su = n
while True:
su = sqre(su)
if su == 1 or su in st:
if s:
st.add(s[0])
return n
if su in s or su in sf:
if s:
sf.add(s[0])
break
s.append(su)
def performant_numbers(n):
"""
Function returns list of Happy numbers between 1 to n
"""
if n < 5000:
return [i for i in range(n+1) if ishappy(i) is not None]
rs = []
with Pool(4) as p:
for i in p.map(ishappy, range(n+1)):
if i is not None:
rs.append(i)
return rs
if __name__ == "__main__":
l = [
59090,114742,104721,194554,15314,293211,198561,118033,243374,138837,73094,33641,105638,281895,52600,193311,214239,66672,188339,221677,87299,127372,74204
]
start = time.time()
for i in l:
performant_numbers(i)
print(time.time() - start)