This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
34.py
46 lines (32 loc) · 1.44 KB
/
34.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
from itertools import (count,
filterfalse)
from math import factorial
from typing import Iterable
from utils import (max_number,
number_to_digits)
digits_factorials = {digit: factorial(digit)
for digit in range(10)}
def exists_digits_factorials_sum_number(digits_count: int) -> bool:
return digits_factorials[9] * digits_count * 9 >= max_number(digits_count)
def max_digits_factorials_sum_digits_count() -> int:
return next(filterfalse(exists_digits_factorials_sum_number,
count(1)))
def is_digits_factorials_sum(number: int) -> bool:
digits_factorials_sum = sum(digits_factorials[digit]
for digit in number_to_digits(number))
return digits_factorials_sum == number
def is_digits_factorials_sum_candidate(number: int) -> bool:
digits = tuple(number_to_digits(number))
zeros_count = digits.count(0)
ones_count = digits.count(1)
last_digit = digits[-1]
return last_digit % 2 == (zeros_count + ones_count) % 2
def digit_factorials() -> Iterable[int]:
digits_count = max_digits_factorials_sum_digits_count()
stop = 10 ** digits_count
candidates = filter(is_digits_factorials_sum_candidate,
range(3, stop))
yield from filter(is_digits_factorials_sum,
candidates)
assert is_digits_factorials_sum(145)
assert sum(digit_factorials()) == 40_730