Codetry

Sift the Two’s and sift the Three’s,
The Sieve of Eratosthenes.
When the multiples sublime,
The numbers that remain are Prime.

Sounds like a abstruse tamil verse where all the wisdoms are hidden?? Yes the above lines also contain a nugget in them ( Sieve of Eratosthenes).

Now whats our job out of it? Much of programming can be mastered by just unearthing the basics and asking few whys and hows in our day to day life (Oops already plenty of such questions are answer pending in my task table:()

Now to the point. As a first step in the process of exploring data structures I am going to implement this sieve primality test in different data structure.

#include <stdio.h>

#define N 1000
int main()

{

int a[N+1],i,j;

for(i=0;i<= N;i++)
a[i]=1;

/* Two is a known prime.Mark on it and eliminate all other friends of two who are multiples of it.
Then three..then five ..so on*/

/* Does that sound like an iteration need to figure out how this can be transformed in code */

for(i=2;i<N/2 +1 ;i++)
for(j=2;j<N/i+1;j++)
{
// printf(“%d Is not a prime \n”,(i*j));
printf(“%d * %d = %d \n”,i,j,i*j);
a[i*j]=0;

}

for(i=0;i<= N;i++)
{
if(a[i])
printf(“%d \n”,i);

}
}

Well though the output is fine there resides bug in this code. Try if you can figure out of it.I will also try to do the primality test using other data structures.

Lets also work on the efficiency of the program. First i need to figure out how to time the execution of the program before patching some improvements to the above code.

So in the todo lists I have,

1.Find how to time

2.Find how can array size be dynamically given

3. Improve the above code

4.Experiment with a source code browser

huh .I restarted blogging..Lets see whether Peerless Inc gets life!!

PS:Nothing much infact nothing is happening at peerless Inc the crippled project should no longer be cited as the reason.Hence I have prepared a plan to prepare GATE plan and help my house hold manager too.. Things are in alpha run.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: