Skip to content

Instantly share code, notes, and snippets.

@anirudhjayaraman
Last active August 31, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anirudhjayaraman/ed3be8a13eb9004a8e8c to your computer and use it in GitHub Desktop.
Save anirudhjayaraman/ed3be8a13eb9004a8e8c to your computer and use it in GitHub Desktop.
Helper Functions used in Solution to Project Euler Problem 12
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, 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment