-
Notifications
You must be signed in to change notification settings - Fork 8
/
092.py
38 lines (30 loc) · 854 Bytes
/
092.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
# time cost = 1.95 ms ± 12.1 µs
from functools import lru_cache
from math import log10
def is_happy(n):
cycle_members = {4, 16, 37, 58, 89, 145, 42, 20}
def get_next(number):
total_sum = 0
while number > 0:
number, digit = divmod(number, 10)
total_sum += digit ** 2
return total_sum
while n != 1 and n not in cycle_members:
n = get_next(n)
return n == 1
@lru_cache(maxsize=None)
def f(n,k):
if n == k == 0:
return 1
elif n < 0:
return 0
elif n > 0 and k ==0:
return 0
else:
return sum([f(n-i**2,k-1) for i in range(10)])
def main(N=10**7):
power = int(log10(N))
u = 9**2*power
happy_numbers = [i for i in range(1,u+1) if is_happy(i)]
ways = sum([f(x,power) for x in happy_numbers])
return (N - ways - 1)