Solving the Project Euler Problems – Problem 6

The sum of the squares of the first ten natural numbers is, 12 + 22 + … + 102 = 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.

No gists found

This one oddly seems relatively simple. simply take a number, work out the sum of 1 to that number then square it, work out the sum of the squares of 1 to that number and find the difference between your 2 results. Job done!

Solving the Project Euler Problems – Problem 5

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?

This one isn’t so much a problem of programming as it is mathematics, as it isn’t just a case of multiplying all the numbers from 1 to 20, that would just result in 20!, which is far bigger than our actual answer.

For reference 20! = 2,432,902,008,176,640,000, which is 2 quintillion 432 quadrillion 902 trillion 8 billion 176 million 640 thousand.

Taking into account the prime numbers up to 20 we have 2, 3, 5, 7, 11, 13, 17 and 19, so our number still needs to be divisible by 4, 6, 8, 9, 10, 12, 14, 15, 16, 18 and 20. Our number so far is 2 × 3 × 5 × 7 × 11 × 13 × 17 × 19.

By virtue of being multiples of these prime factors, our number is already divisible by 6 (2 × 3), 10 (2 × 5) and 14 (2 × 7). The numbers now remaining are 4, 8, 9, 12, 16, 18 and 20.

If we multiply our number by 2, it is now divisible by 4 (it was divisible by 2), 12 (it was divisible by 6) and 20 (it was divisible by 10). The numbers now remaining are 8, 9, 16 and 18 and our number is 2 × 2 × 3 × 5 × 7 × 11 × 13 × 17 × 19.

Multiplying by 2 again will make our number divisible by 8, and doing it again will make it divisible by 16, leaving only 9 and 18 and making our number 2 × 2 × 2 × 2 × 3 × 5 × 7 × 11 × 13 × 17 × 19.

Now multiplying our number by 3, as it was already divisible by both 3 and 6, will now make it divisible by 9 and 18.

Our number is now 2 × 2 × 2 × 2 × 3 × 3 × 5 × 7 × 11 × 13 × 17 × 19 = 116,396,280. Far smaller than our result for 20! above!

Incidentally, by virtue of being divisible by 3 and 7, our number is also divisible by 21. And as it’s divisible by 2 and 11, it is also divisible by 22.

Solving the Project Euler Problems – Problem 4

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.

No gists found

Python’ s very nice way of manipulating integers into strings and string into integers was very helpful with this one, all I had to do was create a function to check if a number is a palindrome (to keep the code neat) and use this to verify our results. If this function returns true and the product of our two numbers is larger than our current result, simply overwrite our result.