-
Notifications
You must be signed in to change notification settings - Fork 17
/
PrimeNumber.java
48 lines (47 loc) · 1.05 KB
/
PrimeNumber.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
package SummerTrainingGFG.Mathematics;
/**
* @author Vishal Singh */
public class PrimeNumber {
static boolean isPrime(int n){
if (n<=1){
return false;
}
if (n<=3){
return true;
}
if (n%2 == 0 || n%3 ==0){
return false;
}
for (int i = 5; i*i <= n ; i=i+6) {
if (n%i == 0 || n%(i+2) == 0){
return false;
}
}
return true;
/*if(n<=1){
return false;
}
if(n<=3){
return true;
}
if (n%2 == 0 || n%3 ==0){
return false;
}
int res = 0;
while (Math.sqrt(n)>0){
res += (n&1);
n = n>>1;
}
if(res == 2 || res == 3){
return true;
}
return false;*/
}
public static void main(String[] args) {
for (int i = 0; i < 50 ; i++) {
if (isPrime(i)){
System.out.println(i+". "+Integer.toBinaryString(i));
}
}
}
}