Last active
August 29, 2015 14:19
-
-
Save is8ac/c119e4027423c9e53fca to your computer and use it in GitHub Desktop.
My python to the solve the problems on Project Euler.
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
# Problem 1 | |
# sum is the sum of the numbers so far. | |
sum = 0 | |
# n is the number that is being tried. | |
n = 0 | |
# While n is less then 1000, | |
while (n < 1000): | |
# And when n/3 or n/5 has a remainder of 0, | |
if n % 3 == 0 or n % 5 == 0: | |
# add n to sum. | |
sum += n | |
# Then increment n. | |
n += 1 | |
# Print the answer. | |
print("sum =",sum) | |
# Problem 2 | |
# fibA and fibB are the two numbers that are to be summed, | |
fibA = 1 | |
fibB = 2 | |
# and fibC is the output. | |
fibC = 3 | |
# sum is the sum of the Fibonacci numbers so far computed. | |
# It starts at 2 because the second Fibonacci number will not be included otherwise. | |
sum = 2 | |
# As long as the Fibonacci numbers are less then 4000000, | |
while (fibC < 4*10**6): | |
# generate the next number as the sum of the two previous numbers. | |
fibC = fibA + fibB | |
# If it is even, | |
if fibC % 2 == 0: | |
# add it to sum. | |
sum += fibC | |
# Then set fibA and fibB back one tick so that you can start over. | |
fibA = fibB | |
fibB = fibC | |
# Print the sum. | |
print(sum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now updated with problem 2.