Q1: 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.
A1: 233168
s=0
for i in range(1,1000):
if i%3==0 and i%5!=0:
s+=i
if i%5==0:
s+=i
i=i+1
print(s)
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.
A2: 4613732
a,b=0,1
s=0
while a<4000000:
if(a%2==0):
s+=a
a,b = b, a+b
print(s)
Q3: The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
A3: 6857
import math
def is_prime(n):
if n == 2 or n == 3: return True
if n < 2 or n%2 == 0: return False
if n < 9: return True
if n%3 == 0: return False
r = int(n**0.5)
f = 5
while f <= r:
if n%f == 0: return False
if n%(f+2) == 0: return False
f +=6
return True
for i in range(2,int(math.sqrt(600851475143)),1):
if(600851475143%i==0 and is_prime(i)):
print(i)
Q4: 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.
A4: 906609
def is_palindromic(n):
a=len(str(n))
b=str(n)
for i in range(int(a/2)):
if(b[i]!=b[a-i-1]):
return False
return True
max=10000
for i in range(100,999):
for j in range(100,999):
if is_palindromic(i*j):
if(max<i*j):
max=i*j
print(max)
Q5: 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?
A5: 232792560def gcd(a,b):
c=1
for j in range(2,a):
if(b%j==0 and a%j==0):
c=j
return c
m=1
for i in range (2,20):
m=m*(int(i/gcd(m,i)))
print(m)