Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Sieve of Eratosthenes Revisited
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { private boolean vS[]=new boolean [1005];// instance variables are initialized to false private int theSieve[] =new int [1005];//you can go crazy with these numbers public void assign() { for (int i=1;i<=1000;i++)// the set of all natural numbers in the array theSieve[i]=i; } public void cancel() { for (int i=2;i<=1000;i++) { if (vS[i]==false)//if a current array index is false ie the index is prime then <<<---- { for (int j=2*i;j<=1000;j+=i)// all multiples of that index must be composite!!!! vS[j]=true; } } }//end method cancel public void displaySieve() { for (int i=2;i<=1000;i++) { if (vS[i]==false) System.out.print(" "+theSieve[i]); } } public static void main (String args[]) { Rextester sieve1=new Rextester(); sieve1.assign(); sieve1.cancel(); sieve1.displaySieve(); } }//end class Rextester (Sieve of Eratosthenes) //PS: vS mean virtual Sieve
run
|
edit
|
history
|
help
0
1.7
contoh 43
Street light
Человечки
Odd
Problem_fib
pk3
RevArray
4.d
jb7.0 extends.override