Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

C Source Code


Welcome to Dream.In.Code
Become an Expert!

Join 244,265 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,171 people online right now. Registration is fast and FREE... Join Now!





Finding Prime Numbers Using The Sieve Of Eratosthenes

This program uses a simple method developed by Eratosthenes(Greek mathematician). Basically u select a number an strike of all its multiplies. then u go to the next number n so on till u reach the end. What ure left with r prime numbers.

Submitted By: Louisda16th
Actions:
Rating:
Views: 14,365

Language: C

Last Modified: August 12, 2006
Instructions: compile, build n run

Snippet


  1. /* Finding Prime Numbers Using The Sieve Of Eratosthenes*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.      int num[100], i, j;
  6.      
  7.      for(i=0;i<=99;i++)     /*Fill num with the first 100 numbers*/
  8.           num[i]=i+1;
  9.      
  10.      for(i=1;i<=99;i++)
  11.      {
  12.           if(num[i]!=0)
  13.           {
  14.                for(j=(i+1);j<=99;j++)
  15.                {
  16.                     if(num[j]!=0)
  17.                     {
  18.                          if((num[j]%num[i])==0)     /*check if num[j]*/
  19.                          num[j]=0;                    /*is a multiple of num[i]*/
  20.                                                        /*if it is a multiple then set it to 0*/
  21.                     }
  22.                }
  23.           }
  24.      }
  25.  
  26.      for(i=0;i<=99;i++)
  27.      {
  28.           if(num[i]!=0)                    /*Print all non zero numbers (prime numbers)*/
  29.                printf("\n%d",num[i]);
  30.      }
  31. }
  32.  
  33.  

Copy & Paste


Comments


NSLogan 2009-03-24 16:01:41

poor scalability


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month