Project Euler
Task 1 Multiples of 3 and 5
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$.
top_num = 1000
nums = set(range(0,top_num, 3)) | set(range(0,top_num, 5))
print(sum(nums))
# Result is 233168
$$ 3+5+6+9+10 \cdots+996=233168 $$
Task 2 Even Fibonacci Numbers
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 \dots $$ By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
n = 4000000
vals = [1,2]
while True:
if vals[-1] + vals[-2] < n:
vals.append(vals[-1] + vals[-2])
else:
break
evenVals = filter(lambda x: x % 2 == 0, vals)
output = sum(list(evenVals))
print(output)
# output is 4613732
Task 3 Largest Prime Factor
The prime factors of $13195$ are $5,7,13,29$ what is the largest prime factor of $600851475143$
num = 600851475143
vals = set(range(3, int(num ** 0.5), 2))
primes = [1]
while len(vals) > 1 :
primes.append(min(vals))
tempFilter = set(range(min(vals), int(num ** 0.5), min(vals)))
vals = vals - tempFilter
possibleFactors = primes
while True:
if num % max(possibleFactors) == 0:
print(max(possibleFactors))
break
else:
possibleFactors.remove(max(possibleFactors))
#6857
Task 4 palindrome number
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is $9009 = 91 \times 99$ . Find the largest palindrome made from the product of two 3-digit numbers.
def isPalindrome(num):
num = [int(digit) for digit in str(num)]
return num == num[::-1]
a = 999
b = 999
while True:
if isPalindrome(a * b):
print(a * b)
print(a)
print(b)
break
else:
if a == 100:
a = 999
b -= 1
else:
a -= 1
# 583 * 995 = 580085
Task 5 smallest multiple
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is $9009 = 91 \times 99$ . Find the largest palindrome made from the product of two 3-digit numbers.
divisor = 2
number = 1
while divisor < 20:
number +=1
divisor = 2
while True:
if number % divisor != 0:
break
else:
divisor +=1
print(f"The number {number} is divisable by all numbers up to {divisor - 1}")
# 232792560
sum square difference
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is $9009 = 91 \times 99$ . Find the largest palindrome made from the product of two 3-digit numbers.
num = 100
SquareOfSum = sum(range(num + 1)) ** 2
sumOfSquares = sum([i ** 2 for i in range(num + 1)])
print(f"{SquareOfSum} - {sumOfSquares} = {SquareOfSum - sumOfSquares}" )
# 25502500 - 338350 = 25164150