-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathsolution_2.py
More file actions
45 lines (37 loc) · 1.11 KB
/
solution_2.py
File metadata and controls
45 lines (37 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
# Good Solution found on forum thread in the projecteuler
from functools import reduce
def divisors(x):
'''
exponents(28) --> 6 because 28 = 2**2 * 7*1:
total number of divisors of 28: (2+1)*(1+1) = 6
'''
expList = []
count = 0
divisor = 2
while divisor <= x:
while x % divisor == 0:
x = x/divisor
count += 1
if count != 0:
expList.append(count+1)
divisor += 1
count = 0
return reduce(lambda x, y: x * y, expList, 1)
# Find the first triangle number to have over n divisors
def diviTri(n):
'''
Triangle numbers = sum of all previous the natural numbers
Ex: The 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
The first triangular number to have over 5 divisors is 28,
whose divisors are 1, 2, 4, 7, 14, 28
'''
natural = 1
triangular = 0
while True:
triangular += natural
natural += 1
if divisors(triangular) > n:
break
# print "First triangular number to have over", n, "divisors:", triangular
return triangular
print(diviTri(500))