-
Notifications
You must be signed in to change notification settings - Fork 2
/
ForThePrimeNumbers.java
44 lines (40 loc) · 1.16 KB
/
ForThePrimeNumbers.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
package com.company;
public class ForThePrimeNumbers {
// prime numbers are the numbers
// who divide with 1 and themselves only
static boolean isPrime(int n){
for(int i=2;i<n;i++) {
if(n%i==0)
return false;
}
return true;
}
static boolean isPrime3(int n){
for(int i=2;2*i<n;i++) {
if(n%i==0)
return false;
}
return true;
}
static boolean isPrime2(int n){
int numerator = 1;
for(int i=2;i<=n;i++) {
if(n%i==0)
numerator++;
}
boolean result = numerator > 2 || numerator == 1 ? false : true;
System.out.println("numerator = " + numerator);
System.out.println("result = " + result);
return result;
}
public boolean isPrimeSqrt(int n) {
if (n <= 1) //get rid of 1 and above
return false;
for (int i = 2; i <= Math.sqrt(n); i++) { //sqrt -> all multiple of the same number should be crossed out
if (n % i == 0)
return false;
}
System.out.println(n + " is prime");
return true;
}
}