Last active
August 31, 2015 17:39
-
-
Save anirudhjayaraman/ed3be8a13eb9004a8e8c to your computer and use it in GitHub Desktop.
Helper Functions used in Solution to Project Euler Problem 12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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