forked from williamfiset/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsPrime.java
31 lines (26 loc) · 761 Bytes
/
IsPrime.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
/**
* Tests whether a number is a prime number or not Time Complexity: O(sqrt(n))
*
* @author Micah Stairs, William Fiset
*/
package com.williamfiset.algorithms.math;
public class IsPrime {
public static boolean isPrime(final long n) {
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
long limit = (long) Math.sqrt(n);
for (long i = 5; i <= limit; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println(isPrime(5));
System.out.println(isPrime(31));
System.out.println(isPrime(1433));
System.out.println(isPrime(8763857775536878331L));
}
}