Wednesday, March 30, 2011

Sieve of Eranthoses - Prime Number Algorithm


In the land of Egypt, Numbers were born.Natural Numbers,Whole Numbers,Positive Numbers,Negative Numbers,Rational Numbers,Irrational Numbers.And something strange happened then.No one believed what happened at that time.All Numbers decide to distribute themselves proportionately but few Numbers resisted.As they didn't distribute their value, their own value was retain by themselves and were considered Primes.
Soon they realize there could be more who can join their group.One, who cant distribute his value was given a task to find prime numbers from whole number group.
Time was flying and whole number group was unrest.Those who found there identity as Prime numbers were troubled by whole numbers.One was given a big responsibility and so he came up with easy logic.

He called each number from whole number group.
Divided that number with all numbers between 1 and number to be check.
If number was divisible by any of previous number it was put into whole number group.
Those who failed to divide themselves were given identity of Prime.

public boolean isPrime(int n) {
for(int i = 2; i < n;++i) {
if(n % i == 0) return true;
}
return false;
}

Prime Numbers were happy as they were getting their own identity.Few days later, first few prime numbers decided to celebrate and they went to One to invite him.To their surprise they found that one was still busy separating other Prime Numbers from Whole Numbers.Primes decided to help One.They called Eratosthenes,ancient Greek mathematician for help.

Eratosthenes asked whole numbers to line up. Few came up and were standing in line.
Counting started from Two as Two was first Prime Number
Two was asked by Eratosthenes to eliminate all numbers standing in line which are multiples of Two.
Next number left after first elimination was Prime again.
This continue for while,Numbers didn't know when to stop.
They asked Eratosthenes, when should they stop elimination.
He suggested :
Number, You multiply you by yourself.
If the Number you are getting after multiplication is less than Number standing last in the queue, you continue elimination.
And if it is greater,stop the elimination.

Algorithm : 
1 . Create a list of consecutive integers from two to n: (2, 3, 4, ..., n).
2 . Initially, let p equal 2, the first prime number.
3 . Starting from p, count up by p and cross out thus found numbers in the list (that will be 2p, 3p, 4p, etc.).
4 . Find the first number not yet crossed out after p (this number is the next prime); replace p with this number.
5 . Repeat steps 3 and 4 until p is greater than n.
6 . All the numbers in the list which are not crossed out are prime.

But difficulty here was all numbers cant stand in a single row at a same time.so Big whole numbers still don't know whether they are Prime or not

History became legend,legend became myth, Time of prime Numbers has now come.They are now basic building blocks of natural numbers,used in many encryption techniques etc.After knowing the importance of Prime Numbers, Whole Number offered Prime number to remain in their group.Prime numbers now belong to both the groups.And all numbers lived happily ever after.

** Java Code size is large so code will be sent on request.All request can be made at satyajit.bhadange@gmail.com or leave the comment with your email id to get Java code for Sieve of Eratosthenes algorithm.

2 comments:

  1. Prime numbers are a beauty and this algorithm to find them is also a beauty.Thanks for sharing.

    ReplyDelete
  2. Hey..that is a good explanation.Thanks for sharing this informative number story.

    - Anup Junagade

    ReplyDelete