waltermilner

Untitled

Sep 21st, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.46 KB | None | 0 0
  1. public class Test {
  2.  
  3.   public static boolean isPrime(int n) {
  4.       for (int i = 2; i <= Math.sqrt(n); i++) // check possible factors
  5.       {
  6.         if (n % i == 0) { // i is a factor
  7.           return false; // so n is not prime
  8.         }
  9.       }    
  10.     return true; // no factors so it is prime
  11.   }
  12.  
  13.   public static void main(String[] args) {
  14. for (int i=2; i<100; i++) // test - all primes less than 100
  15.       if (isPrime(i)) System.out.println(i);
  16.   }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment