I recently discovered projecteuler.net, where you can find mathematical/computer programming problems and solve them.
Lately I’ve been trying to learn more Python, so I decided to give it a try - solving Euler problems with Python.

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.My solution:
1 2 3 4 5 6 7 8 9 10 11 12 | |
It basically runs 2 loops to generate the numbers and each calculated number is turned into a string, reversed, turned back into an integer and compared with the original number. One thing I do not like about the code already is that it uses an additional array, which can be removed with a simple if statement (might not even need that either). It needs refactoring, but it got me the answer, so - moving on.
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?
My solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
This one took some time. Because of my brute force approach, it took ~ 5 minutes to complete. I knew that lowest common multiple needed to be found, but because as far as I know LCM is computed for 2 numbers at a time, I couldn’t come up with a quicker algorithm in a few minutes. Rather it became a brute force algorithm, not very effective, but got the job done.
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.
My solution:
1 2 3 | |
I think this solution is the best so far. Solved in 3 lines, can be easily merged into a single line. Range generates a list of numbers [1, 2, 3… 99, 100]. Using list comprehension I use the same list and square each list element, that also returns a list, which sum function uses to calculate total sum of squares. ** is used instead of a math.pow function.