Skip to content

Commit

Permalink
Added MillerRabin
Browse files Browse the repository at this point in the history
  • Loading branch information
EricSzla committed Oct 31, 2017
1 parent 628f2be commit 2b1967e
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 0 deletions.
1 change: 1 addition & 0 deletions MillerRabin/.idea/description.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions MillerRabin/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions MillerRabin/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions MillerRabin/.idea/project-template.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

217 changes: 217 additions & 0 deletions MillerRabin/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions MillerRabin/MillerRabin.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.
Binary file not shown.
83 changes: 83 additions & 0 deletions MillerRabin/src/com/company/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.company;

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

/* Code developed by Eryk Szlachetka
* Implementation of Miller-Rabin algorithm,
* to check if the number is prime or not.
* If the algorithm returns Composite then the number is probably prime.
* If the algorithm returns inconclusive, then it is not prime.
*/

public class Main {

public static void main(String[] args) {

String prime = "";
long n;
boolean isPrime;

Scanner scan = new Scanner(System.in);
System.out.println("Enter a number to check if it is prime: ");
n = scan.nextLong();
System.out.println("Checking...\n");
isPrime = checkIfPrime(n);

if(isPrime){
prime = "Composite";
}else{
prime = "Inconclusive";
}

System.out.println("The number: " + n + " is: " + prime);

}

private static boolean checkIfPrime(long n){

// Return false if n is 0 or 1 as its not prime
if(n == 0 || n == 1){
return false;
}
// Return true if n is 2, as it is prime
if(n == 2){
return true;
}
// If its an even number other than 2 then return false.
if(n % 2 == 0){
return false;
}


// Find integers k, q with k > 0, q is odd so that (n - 1 = 2^k q)
long q = n-1;
int k = 0;

while(q % 2 == 0){ // while q is odd
q = q/2; // divide q by 2
k++; // increment k
}
// Select random integer a where 1 < a < n-1
long a = ThreadLocalRandom.current().nextLong(1, (n-1));

// If a^q mod n == 1 then return "inconclusive"
if(((a^q) % n) == 1){
return false;
}

for(int j =0; j < k-1; j++){
// if a^((2^j)q) mod n == n - 1 then return "inconclusive"
long power = 2^j * q;
long res = a^power;

if(res % n == n-1) {
return false;
}
}
// Return "composite"
return true;
}
}


0 comments on commit 2b1967e

Please sign in to comment.