Saturday, July 1, 2017

Prime Number

import java.util.stream.IntStream;

public class PrimeNumber {
    
     public static void main(String[] args) {
          PrimeNumber primeNumber = new PrimeNumber();
          System.out.println(primeNumber.isPrime(2));
          System.out.println(primeNumber.isPrime(9));
     }
    
     public boolean isPrime(int number) {
          if (number > 1) {
               return !IntStream.rangeClosed(2, (int) Math.sqrt(number)).anyMatch(x -> number % x == 0);
          }
          return false;
     }
}


Output:
true
false

No comments:

Post a Comment