forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_prime.java
49 lines (45 loc) · 1.4 KB
/
check_prime.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
Prime Numbers are those who dont's have any divisors other than 1 and itself,
so, the number (n) which has any divisors in between 2 and (n-1) is not Prime Number
so, firstly consider special case of 2 (2 is the only even prime)
then, all other even No. except 2 as Not Prime
Thus, we will check from 3 to (n-1) (all odd No. )for reminder to be 0, if reminder comes 0 at any one position
so that will not be prime No. if not at any single position then it will reach upto end of isPrime Method & will be prime.
*/
package primeno;
import java.util.Scanner;
public class CheckPrime {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter No : ");
int n = scan.nextInt();
System.out.println(isPrime(n));
}
public static boolean isPrime(int n) {
if (n == 2){
return true;
} else if (n%2==0){
return false;
}else if (n<=1){
return false;
}
for (int i =3 ;i<Math.sqrt(n) ; i+=2){
if (n%i==0){
return false;
}
}
return true;
}
}
/*
Test Cases:
Input: 5
Output: true
Input: 6
Output: false
One Exception : 2 is the only even prime (Taken Special case)
Input: 2
Output: true
Time Complexity: O(n)
Space Complexity: O(1)
*/