Advertisement
calcpage

C6X7_PrimeGen.java

Jan 13th, 2012
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.46 KB | None | 0 0
  1. //PrimeGen.java     MrG 2012.0113
  2. public class PrimeGen
  3. {
  4.     private int current;
  5.  
  6.     public PrimeGen()
  7.     {
  8.         current = 1;
  9.     }
  10.  
  11.     public int nextPrime()
  12.     {
  13.         do
  14.         {
  15.             current++;
  16.         }
  17.         while(!isPrime());
  18.         return current;
  19.     }
  20.  
  21.     private boolean isPrime()
  22.     {
  23.         if(current<2){return false;}
  24.         if(current==2){return true;}
  25.         if(current%2==0){return false;}
  26.         for(int i=3; i*i<=current; i+=2)
  27.         {
  28.             if(current%i==0){return false;}
  29.         }
  30.         return true;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement