- public class PrimeNumberAlgorithm {
- public static void main(String[] args) {
- isPrimeNumber(11);
- }
- private static void isPrimeNumber(int n) {
- boolean isPrime = true;
- int m = n / 2;
- for (int i = 2; i <= m; i++) {
- if (n % i == 0) {
- isPrime = false;
- break;
- }
- }
- System.out.println(isPrime == true ? "Prime number !!" : "Not prime number !!");
- }
- }
Time complexity is O(n/2)
No comments:
Post a Comment