Last active
May 5, 2016 16:56
-
-
Save anivarth/da7ceb6b0692bc3d40e0964f5175abd6 to your computer and use it in GitHub Desktop.
Project Euler Problem 5 Solution with Python
This file contains 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
#http://radiusofcircle.blogspot.com | |
#time module for execution time | |
import time | |
#Time at the start of program | |
start_time = time.time() | |
#We have used euclid Algorithm to find the | |
#lcm also called smallest multiple | |
#gcd function for calculating lcm | |
def gcd(n1,n2): | |
remainder = 1 | |
while remainder != 0: | |
remainder = n1%n2 | |
n1 = n2 | |
n2 = remainder | |
return n1 | |
#lcm of two numbers using euclid Algorithm | |
#lcm = (number1*number2)/GCD(number1,number2) | |
def lcm(n1,n2): | |
return (n1*n2)/gcd(n1,n2) | |
#lcm of 2,3 | |
#You can also use (2*3)/gcd(2,3) | |
l = lcm(2,3) | |
#lcm of three numbers n1,n2,n3 is | |
#lcm(lcm(n1,n2),n3) | |
for i in range(4,21): | |
l = lcm(l,i) | |
#Printing the lcm at the end | |
print l | |
#time at the end of program | |
end_time = time.time() | |
#total Time taken | |
print end_time - start_time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment