Sunday, February 27, 2011

Sieve of Eratosthenes

The Sieve of Eratosthenes is a method of finding prime numbers.
Basic steps in Algorithm.
1.Create an array with all elements initialized to 1. Array elements with prime subscripts will remain 1. others will eventually become 0.
2. Start from subscript 2, every time an array element is found with value 1, loop through the remainder of the array and set to 0 whose subscript is multiple of subscript which already found value with 1.
3. it is enough to find subscript with value 1 until sqrt(ARRAY_SIZE).
to know more about this method click here........
#################################################################
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE 1000
/*
*
*/
int main(int argc, char** argv) {
int prime[SIZE]={0};
int i;
for(i=0;i<SIZE;i++){
prime[i]=1;
}
for(i=2;i<sqrt(SIZE);i++){
int j;
if(prime[i]==1){
for(j=i+1;j<SIZE;j++){
if((j%i)==0){
prime[j]=0;
}
}
}
}
for(i=0;i<SIZE;i++){
if(prime[i]==1){
printf("%d\n",i);
}
}
return (EXIT_SUCCESS);
}

Wednesday, February 23, 2011

Hello World

Hi This post is for testing my blog.
Still No plans on my mind on what to upload ;)