Skip to content

Commit cf14473

Browse files
author
Christian Bender
authored
Merge pull request TheAlgorithms#281 from daniel-s-ingram/master
Thanks for contribution
2 parents b4cbf5d + 53d9989 commit cf14473

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Project Euler/Problem 12/sol1.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from __future__ import print_function
2+
from math import sqrt
3+
'''
4+
Highly divisible triangular numbers
5+
Problem 12
6+
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
7+
8+
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
9+
10+
Let us list the factors of the first seven triangle numbers:
11+
12+
1: 1
13+
3: 1,3
14+
6: 1,2,3,6
15+
10: 1,2,5,10
16+
15: 1,3,5,15
17+
21: 1,3,7,21
18+
28: 1,2,4,7,14,28
19+
We can see that 28 is the first triangle number to have over five divisors.
20+
21+
What is the value of the first triangle number to have over five hundred divisors?
22+
'''
23+
try:
24+
xrange #Python 2
25+
except NameError:
26+
xrange = range #Python 3
27+
28+
def count_divisors(n):
29+
nDivisors = 0
30+
for i in xrange(1, int(sqrt(n))+1):
31+
if n%i == 0:
32+
nDivisors += 2
33+
34+
return nDivisors
35+
36+
tNum = 1
37+
i = 1
38+
39+
while True:
40+
i += 1
41+
tNum += i
42+
43+
if count_divisors(tNum) > 500:
44+
break
45+
46+
print(tNum)

0 commit comments

Comments
 (0)