Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S-12345-add-new-algo #359

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.williamfiset.algorithms.math;

import java.util.Scanner;

public class RandomizedIsPrime{

private static final Integer ITERATION = 20;

public static boolean isPrime(long n, int iteration) {
long a;

long up = n - 2;
long down = 2;
for (int i = 0; i < iteration; i++) {
a = (long) Math.floor(Math.random() * (up - down + 1) + down);
if (modPow(a, n - 1, n) != 1) {
return false;
}
}
return true;
}

/**
* *
*
* @param a basis
* @param b exponent
* @param c modulo
* @return (a ^ b) mod c
*/
private static long modPow(long a, long b, long c) {
long res = 1;
for (int i = 0; i < b; i++) {
res *= a;
res %= c;
}
return res % c;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

long n = scanner.nextInt();
if (isPrime(n, ITERATION)) {
System.out.println(n + " is Prime");
} else {
System.out.println(n + " isn't Prime");
}

}

}