-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.py
46 lines (40 loc) · 737 Bytes
/
12.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
'''
Euler problem 12.
Thomas Scrace
March 2012
'''
'''
What is the first triangle number to have over 500 divisors?
'''
def num_divisors(n):
m = 2
d = 2
while m <= n / 2:
if n % m == 0:
d += 1
m += 1
return d
def get_triangle(n):
t = 0
for i in range(1, n + 1):
t += i
print "\t triangle is %d" % t
return t
def get_tri(n):
low = 2
high = 2
while True:
print "Trying %d" % high
high_div = num_divisors(get_triangle(high))
print "\t\t %d divisors" % high_div
if high_div >= n:
if high_div == n:
candidate = n
while num_divisors(get_triangle(candidate)) == n:
candidate -= 1
return candidate + 1
high = low + ((high - low) / 2)
else:
low = high
high *= 2
print get_tri(501)