aboutsummaryrefslogtreecommitdiff
path: root/challenge-168/adam-russell/java/ch-2.java
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2022-06-12 12:37:18 -0400
committerAdam Russell <ac.russell@live.com>2022-06-12 12:37:18 -0400
commit76fb2113ab5563b3d142fc1f66ffc1b64fced916 (patch)
tree458861902116f336c2eaa2b605fd326f31769b84 /challenge-168/adam-russell/java/ch-2.java
parentad6edd6a47450455cf81a94854ce74eabee2501a (diff)
downloadperlweeklychallenge-club-76fb2113ab5563b3d142fc1f66ffc1b64fced916.tar.gz
perlweeklychallenge-club-76fb2113ab5563b3d142fc1f66ffc1b64fced916.tar.bz2
perlweeklychallenge-club-76fb2113ab5563b3d142fc1f66ffc1b64fced916.zip
initial commit
Diffstat (limited to 'challenge-168/adam-russell/java/ch-2.java')
-rw-r--r--challenge-168/adam-russell/java/ch-2.java89
1 files changed, 89 insertions, 0 deletions
diff --git a/challenge-168/adam-russell/java/ch-2.java b/challenge-168/adam-russell/java/ch-2.java
new file mode 100644
index 0000000000..ed2906c49f
--- /dev/null
+++ b/challenge-168/adam-russell/java/ch-2.java
@@ -0,0 +1,89 @@
+import java.util.Random;
+import java.util.ArrayList;
+import java.math.BigInteger;
+import java.util.Collections;
+
+/** Class MillerRabin **/
+/* Slightly modified version of code taken from
+ https://www.sanfoundry.com/java-program-miller-rabin-primality-test-algorithm/ */
+class MillerRabin{
+ private static final int ITERATIONS = 7;
+ /** Function to check if prime or not **/
+ public static boolean isPrime(long n){
+ /** base case **/
+ if (n == 0 || n == 1)
+ return false;
+ /** base case - 2 is prime **/
+ if (n == 2)
+ return true;
+ /** an even number other than 2 is composite **/
+ if (n % 2 == 0)
+ return false;
+ long s = n - 1;
+ while (s % 2 == 0)
+ s /= 2;
+ Random rand = new Random();
+ for (int i = 0; i < ITERATIONS; i++){
+ long r = Math.abs(rand.nextLong());
+ long a = r % (n - 1) + 1, temp = s;
+ long mod = modPow(a, temp, n);
+ while (temp != n - 1 && mod != 1 && mod != n - 1){
+ mod = mulMod(mod, mod, n);
+ temp *= 2;
+ }
+ if (mod != n - 1 && temp % 2 == 0)
+ return false;
+ }
+ return true;
+ }
+ /** Function to calculate (a ^ b) % c **/
+ public 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;
+ }
+ /** Function to calculate (a * b) % c **/
+ public static long mulMod(long a, long b, long mod){
+ return BigInteger.valueOf(a).multiply(BigInteger.valueOf(b)).mod(BigInteger.valueOf(mod)).longValue();
+ }
+}
+
+class HomePrime{
+ private static ArrayList primeFactors(long n){
+ ArrayList factors = new ArrayList();
+ while (n % 2 == 0){
+ factors.add(new Long(2));
+ n = n / 2;
+ }
+ for(long i = 3; i <= Math.sqrt(n); i = i + 2){
+ while (n % i == 0){
+ factors.add(new Long(i));
+ n = n / i;
+ }
+ }
+ if(n > 2)
+ factors.add(new Long(n));
+ return factors;
+ }
+
+ public static long homePrime(long n){
+ ArrayList primeFactors = primeFactors(n);
+ String s = "";
+ for(int i = 0; i < primeFactors.size(); i++){
+ s += ((Long) primeFactors.get(i)).toString();
+ }
+ if(MillerRabin.isPrime(Long.valueOf(s).longValue())){
+ return Long.valueOf(s).longValue();
+ }
+ return homePrime(Long.valueOf(s).longValue());
+ }
+
+ public static void main(String[] args){
+ System.out.println(HomePrime.homePrime(10));
+ System.out.println(HomePrime.homePrime(16));
+ System.out.println(HomePrime.homePrime(48));
+ }
+} \ No newline at end of file