-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem 12.py
More file actions
executable file
·31 lines (24 loc) · 868 Bytes
/
Problem 12.py
File metadata and controls
executable file
·31 lines (24 loc) · 868 Bytes
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
#!/usr/bin/env python
__author__ = 'Ned Udomkesmalee'
#What is the value of the first triangle number to have over five hundred divisors?
def num_factors(n):
return len(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def problem12():
print("Find the first triangle number to have n divisors")
while True:
n = raw_input("What is n? ")
try:
n = int(n)
if n > 0:
next_num = 2
t_num = 1
while num_factors(t_num) < n:
t_num += next_num
next_num += 1
print "Answer = %i" % t_num
break
else:
print "*** n must be positive. ***"
except ValueError:
print "*** %s is not a valid integer. ***" % n
problem12()