-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths012-highly-divisible-triangular-num.py
52 lines (44 loc) · 1.45 KB
/
s012-highly-divisible-triangular-num.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
# Project Euler 12: Highly divisible triangular number
# GitHub: urtuba
from collections import defaultdict
from functools import reduce
from sys import maxsize
def number_of_divisors(number: int) -> int:
'''
Finds number of divisors/factors of a given number.
:param number: number to find its divisors
:return: count of divisors
'''
divisors = defaultdict(lambda: 0)
divisor = 2
while number != 1:
if number % divisor == 0:
number = number / divisor
divisors[divisor] += 1
else:
divisor += 1
return reduce(lambda x,y: x * (y+1), divisors.values(), 1)
def triangular_number_generator() -> int:
'''
Triangular number generator.
:return: next triangular number until sys.maxsize
'''
triangular_number = 1
for counter in range(2, maxsize):
yield triangular_number
triangular_number += counter
def highly_divisible_triangular_n(over: int) -> int:
'''
Finds minimum triangular number that has divisiors over 'over'.
:param over: used in comparison, factor count should be larger than 'over'
:return: minimum number which satisfies function's condition.
'''
trig_gen = triangular_number_generator()
number = 0
while True:
number = next(trig_gen)
if number_of_divisors(number) > over:
return number
if __name__ == '__main__':
result = highly_divisible_triangular_n(500)
print(result)