-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask_12_Highly_Divisible_Triangular_Number.py
57 lines (42 loc) · 1.3 KB
/
Task_12_Highly_Divisible_Triangular_Number.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
"""
https://projecteuler.net/problem=12
The sequence of triangle numbers is generated by adding the natural numbers.
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?
"""
GOAL = 500
def triangle_numbers():
idx = 1
while True:
yield int(idx * (idx + 1) / 2)
idx += 1
def find_prime_numbers(n):
d = 2
numbers = []
while n > 1:
if n % d == 0:
n = n / d
numbers.append(d)
d = 2
continue
d += 1
return numbers
if __name__ == "__main__":
for number in triangle_numbers():
if number == 1:
continue
prime_numbers = find_prime_numbers(number)
prime_numbers_with_powers = {}
for p in prime_numbers:
if p not in prime_numbers_with_powers:
prime_numbers_with_powers[p] = 1
continue
prime_numbers_with_powers[p] += 1
dividers_power = [n + 1 for n in prime_numbers_with_powers.values()]
dividers_count = dividers_power[0]
for p in dividers_power[1:]:
dividers_count *= p
if dividers_count > GOAL:
print(number)
break
# the answer is 76576500