- /*
- * EratosthenesSieve.java
- *
- * Copyright 2010 Ankush Bisht
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- */
- public class EratosthenesSieve {
- public static void main (String args[]) {
- System.out.println("Enter the Number till you want to print the Primes");
- int a[]=new int[new java.util.Scanner(System.in).nextInt()];
- for(int i=0;i<a.length;i++)
- a[i]=i+1;
- int i,j,k,step;
- for(i=1;i<a.length;i++)
- {
- if(a[i]!=0)
- {
- k=a[i]*2-1;
- step=a[i];
- for(j=k;j<a.length;j+=step)
- a[j]=0;
- }
- }
- System.out.println("Prime Numbers between 1 and "+a.length+" are: ");
- for(i=0;i<a.length;i++)
- {
- if(a[i]!=0)
- System.out.println(a[i]+" ");
- }
- }
- }
About Me
Favourites
Sunday, February 14, 2010
EratosthenesSieve : A very fast algorithm for finding Prime numbers.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment