Problem solving - Questions and Solutions

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.

total = 0
for i in range(1, 1000):
    if i % 3 == 0 or i % 5 == 0:
        total += i

print(total)  # 233168

Q2

Define a non-recursive factorial function that computes n!.

Reminder: n! means n × (n − 1) × … × 3 × 2 × 1 Also, by convention, 0! = 1

def factorial(n):
    product = 1
    for n in range(1, n + 1):
        product *= n
    return product

Q3

2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 2^1000?

Hint: in Python, ** is the exponentiation operator, that is to compute a^b, in Python, we write a ** b.

Hint: for loops work with strings too. Try this: for character in “My random string”: print(character)

total = 0
for digit in str(2 ** 1000):
    total += int(digit)
print(total)  # 1366

Q4

10! = 10 × 9 × … × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!

total = 0
for digit in str(factorial(100)):
    total += int(digit)
print(total)  # 648

Q5

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 = 5^2.

There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.


def find_triplet(target_sum):
    for c in range(3, target_sum):
        for b in range(2, c):
            for a in range(1, b):
                if a + b + c == target_sum and a ** 2 + b ** 2 == c ** 2:
                    return a * b * c


print(find_triplet(1000))  # 31875000

Q6

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 = 552 = 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.

lower_bound = 1
upper_bound = 100
sum_squared = int(((upper_bound / 2) * (lower_bound + upper_bound)) ** 2)
sum_of_squares = 0

for n in range(lower_bound, upper_bound + 1):
    sum_of_squares += n ** 2

print(sum_squared - sum_of_squares)  # 25164150

Q7

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.

ante_penultimate = 1
penultimate = 1
current = 2
even_sum = 0

while current <= 4e6:
    current = ante_penultimate + penultimate

    if current % 2 == 0:
        even_sum += current

    ante_penultimate = penultimate
    penultimate = current

print(even_sum)  # 4613732