Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@
* [OddEvenSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java)
* [PancakeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PancakeSortTest.java)
* [PatienceSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PatienceSortTest.java)
* [PigeonholeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/PigeonholeSortTest.java)
* [QuickSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/QuickSortTest.java)
* [RadixSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/RadixSortTest.java)
* [SelectionSortRecursiveTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/SelectionSortRecursiveTest.java)
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/thealgorithms/maths/PrimeCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public static void main(String[] args) {
} else {
System.out.println("algo2 verify that " + n + " is not a prime number");
}

if (isPrimeNumberOptimised(Long.MAX_VALUE)) {
System.out.println("algo4 verify that " + Long.MAX_VALUE + " is a prime number");
} else {
System.out.println("algo4 verify that " + Long.MAX_VALUE + " is not a prime number");
}
scanner.close();
}

Expand Down Expand Up @@ -82,4 +88,27 @@ private static long modPow(long a, long b, long c) {
}
return res % c;
}

/**
* Checks if a given number is prime using an optimized approach.
* @param number the number to check
* @return true if the number is prime, false otherwise
*/
public static boolean isPrimeNumberOptimised(long number) {
// Numbers less than or equal to 1 are not prime
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about removing these comments and moving them to the Javadoc? Because the code of this method is quite simple, but all these descriptions are harder to read: and after deleting it should be:

/**
 * Determines if a given number is prime using an optimized approach.
 * <p>
 * This method checks for primality with the following steps:
 * <ul>
 *   <li>Numbers less than or equal to 1 are not prime.</li>
 *   <li>Numbers 2 and 3 are prime.</li>
 *   <li>Eliminates even numbers and multiples of 3 early for efficiency.</li>
 *   <li>Uses the 6k ± 1 optimization for checking factors, which skips even numbers
 *       and multiples of 3 to cover all other possible factors.</li>
 *   <li>If no factors are found up to the square root of the number, the number is prime.</li>
 * </ul>
 * 
 * @param number the number to check
 * @return true if the number is prime, false otherwise
 */
public static boolean isPrimeNumberOptimised(long number) {
    if (number <= 1) return false;
    if (number <= 3) return true;
    if (number % 2 == 0 || number % 3 == 0) return false;
    for (long i = 5; i * i <= number; i += 6) {
        if (number % i == 0 || number % (i + 2) == 0) {
            return false;
        }
    }
    return true;
}

if (number <= 1) return false;
// Numbers 2 and 3 are prime
if (number <= 3) return true;
// Eliminate even numbers and multiples of 3
if (number % 2 == 0 || number % 3 == 0) return false;
// Check for factors from 5 onwards using the 6k ± 1 optimization
for (long i = 5; (i * i) <= number; i += 6) {
// Check if the number is divisible by i or (i + 2)
if (number % i == 0 || number % (i + 2) == 0) {
return false;
}
}
// If no factors are found, the number is prime
return true;
}
}