Problem
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Solution: (download here)
num = 0
sum = 0
while (num<1000):
if (num % 3) == 0:
sum = num + sum
elif (num % 5) == 0:
sum = num + sum
num += 1
print sum
Problem
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Solution: (download here)
# n is the nth term inside the Fibonacci sequence
# return the vaule of the nth term
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
# choose the subFib from the Fibonacci sequence based upon the given range
# return the sum of the even values between the startNum and endNum
def subFib(startNum,endNum):
sum = 0
n = 0
cur = fib(n)
while cur<= endNum:
if startNum <= cur:
if (cur % 2) == 0:
sum = sum + cur
n += 1
cur = fib(n)
return sum
print subFib(1,4000000)
Problem
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
Solution: (download here)
from math import *
#KEY: there cannot be a number greater than the square root of that number being prime
def isPrime(n):
if n<= 1:
return False
i = 2.0
while (i <= sqrt(n)):
if n%i == 0:
return False
i += 1
return True
def factor(n):
i = 2
list = []
while(i<sqrt(n)):
if isPrime(i) and (n%i == 0):
list.append(i)
i += 1
return list[len(list)-1]
print factor(600851475143)
Problem
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
Solution: (download here)
from math import *
# pure practice for the recursion data structure, nothing to do with the main program
def isPalindrome(num):
if len(num) == 1 or len(num) == 0:
return True
first = num[0]
last = num[len(num)-1]
return (first == last) and isPalindrome(num[1:len(num)-1])
#print "is num %d a Palindrome? %s" % (9909,isPalindrome("9909"))
def find():
list = []
r = 0
i = 999
j = 999
while j >= 100:
while i >= 100:
r = i*j
#KEY: shortcut to reverse string
if str(r) == str(r)[::-1]:
list.append(r)
i -= 1
j -= 1
i = j - 1
list.sort()
return list
print "largest Palindrome from product of the three-digit number is %s" % find()
Problem
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
Solution: (download here)
from math import *
import numpy as np
def isPrime(n):
if n<= 1:
return False
i = 2.0
while (i <= sqrt(n)):
if n%i == 0:
return False
i += 1
return True
def minDivisible(k):
# N: the value the function is going to return
N = 1
# index number
i = 0
check = True
# upper bound of the estimation process for a[i]
limit = sqrt(k)
a = np.zeros(20)
# plist is the prime list
plist = []
j = 1
while j <= k:
if isPrime(j):
plist.append(j)
j += 1
while i < len(plist):
#print "index of the list before increment:%d"%i
a[i] = 1
if check:
if plist[i] <= limit:
a[i] = int( log10(k) / log10(plist[i]) )
else:
check = False
#print "N: %d"%N
N = N * plist[i]**a[i]
i = i + 1
#print "index of the list after increment:%d"%i
#print a
return N
print"the smallest number divisible by each of the numbers 1 to 20 is: %d" % minDivisible(20)
Problem
The sum of the squares of the first ten natural numbers is,
\(1^2 + 2^2 + ... + 10^2 = 385\)
The square of the sum of the first ten natural numbers is,
\((1 + 2 + ... + 10)^2 = 55^2 = 3025\)
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is \(3025 − 385 = 2640\).
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Solution: (download here)
from math import *
def diffSum(n):
sum_square_n = n * (n+1) *(2*n+1) / 6.0
sum_n_square = pow((1+n)*n / 2.0 , 2)
return abs(sum_n_square - sum_square_n)
print diffSum(100)
Problem
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
Solution: (download here)
from math import *
def isPrime(n):
if n<= 1:
return False
i = 2.0
while (i <= sqrt(n)):
if n%i == 0:
return False
i += 1
return True
def find(n):
list = [2]
i = 3
count = 0
while count < n-1:
if isPrime(i):
list.append(i)
count += 1
i += 2
return list[count]
print "the 10 001st prime number is: %d"%find(10001)
Problem
Find the greatest product of five consecutive digits in the 1000-digit number.
73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450
Solution: (download here) (download input file)
import os
# open the file to read
f = open(os.path.abspath('euler8_input.txt'))
# store the 1000 digits
data = ''
# store the parse result from one line
parse = ''
# product of five consecutive digits (final result)
product = 0
# index
i = 0
# product of five consecutive digits (cal result)
result = 0
# parse the input file to form 1000 digits into one string
for line in f:
# note line.strip('\n') will not affect 'line'
parse = line.strip('\n')
data = data + parse
while ((i+4) < len(data)):
result = int(data[i]) * int(data[i+1]) * int(data[i+2]) * int(data[i+3]) * int(data[i+4])
if result > product:
product = result
i += 1
print product
note
string.strip() will not affect the original string.
Problem
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
\(a^2 + b^2 = c^2\)
For example, \(3^2 + 4^2 = 9 + 16 = 25 = 52\).
There exists exactly one Pythagorean triplet for which \(a + b + c = 1000\). Find the product \(abc\).
Solution: (download here)
# transform relations into ab = 1000(a+b)-500*1000
for a in range(0,1001):
for b in range(0,1001):
if (a*b == 1000*(a+b) - 500*1000):
print a*b*(1000-a-b)
break
Problem
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Solution: (download here)
from math import *
def isPrime(n):
if n<= 1:
return False
i = 2.0
while (i <= sqrt(n)):
if n%i == 0:
return False
i += 1
return True
# since we know that 2 and 3 are primes
summation = 5
for i in range(5,2000000):
if isPrime(i):
summation += i
print summation