-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.py
45 lines (39 loc) · 1.39 KB
/
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
from math import *
# Function to calculate the number of divisors of integer n
def divisors(n):
limit = int(sqrt(n))
divisors_list = []
for i in range(1, limit + 1):
if n % i == 0:
divisors_list.append(i)
if i != n/i:
divisors_list.append(n/i)
return len(divisors_list)
# Function to check for triangle number
def isTriangleNumber(n):
a = int(sqrt(2*n))
return 0.5*a*(a+1) == n
# Function to calculate the last term of the series adding up to the triangle number
def lastTerm(n):
if isTriangleNumber(n):
return int(sqrt(2*n))
else:
return None
# First Step
# First number 'check' to have 500 divisors
check = 2**4 * 3**4 * 5**4 * 7 * 11
# Second Step
# Starting from 'check', iterate sequentially checking for the next 'triangle' number
#print("not there yet:", check )
while not isTriangleNumber(check):
check += 1
#print("not there yet:", check )
# Third and Fourth Steps
# Calculate the last term of the series ('seriesLastTerm') that adds up to the newly calculated triangle number 'check'
seriesLastTerm = lastTerm(check)
# Iterate over triangle numbers checking for divisors > 500
while divisors(check) <= 500:
# add the next term to check to get the next triangle number
check += (seriesLastTerm + 1)
seriesLastTerm += 1
print (check)