Long ago, I think 2-3 years ago, I came to know about Project Euler Problems, I made an account, solved 1 or 2 problems, and then never tried them.
Now, I decided to give them a try again. I was solving Project Euler 10 Problem which states that:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Now, I wrote this Python code for the same. I know it's not optimized. Not at all. Why, you'll come to know shortly.
I knew that is terrible and it would not solve this way. So, I researched about the problem and came to know about Sieve of Eratosthenesthrough this question on StackOverflow:
I jumped over to Wikipedia page and understood what this algorithm was actually all about, and I knew, my algo was taking time because it would evaluate a number to be prime or not for each and every number from 1 to 2 million which is processor intensive and redundant.
What does Sieve of Eratosthenes does?
First it creates a list of numbers on which we want to carry some prime number operation for which we want to know if they are prime or not. It takes a number for example 2, calculate if it's prime, and then remove all the elements which are multiple of 2.
Then it jumps to next number, say 3, which is prime, and then removes all it's multiples.
While I was trying to do the calculation of whether a number is prime or not for each and every number till 2 million, this algo would calculate the prime number property for very low amount of numbers. Hence, efficient.
I have solved 15 problems on Project Euler till now :)
Comments !