Guest User

Untitled

a guest
Dec 30th, 2020
31
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.mersenne;
  2.  
  3. import java.util.Scanner;
  4. import java.math.BigInteger;
  5. import java.io.*;
  6. /**
  7. * Java implementation of LL.
  8. **/
  9.  
  10. class SimpleLucas {
  11.   public static void main(String args[]) {
  12.    BigInteger sumBig = BigInteger.valueOf(4);
  13.  
  14.    System.out.println("Enter exponent: ");
  15.  
  16.    Scanner sc = new Scanner(System.in);
  17.    int iIteration = sc.nextInt();
  18.  
  19.    System.out.print("Start calculating ... ");
  20.    if (isPrime(iIteration)==false) {
  21.      System.out.println("Exponent is not prime!");
  22.      return;
  23.      }
  24.    System.out.println("Your exponent "+iIteration+" is prime!");
  25.  
  26.    BigInteger bi1, bi2, MB, minusTwo;
  27.    bi1 = new BigInteger("2");
  28.    bi2 = bi1.pow(iIteration);
  29.    MB = bi2.add(BigInteger.valueOf(-1));
  30.    minusTwo = new BigInteger("-2");
  31.  
  32.    long startCounter = System.currentTimeMillis();
  33.    BigInteger powBig;
  34.  
  35.    System.out.println("Variables is init");
  36.  
  37.     for (int x=1;x<=iIteration-2;x++) {
  38.        long startIt = System.currentTimeMillis();
  39.        powBig = sumBig.pow(2);
  40.        powBig = powBig.add(minusTwo);
  41.        sumBig = powBig.mod(MB);
  42.        if (x==1){
  43.          long stopCounter = System.currentTimeMillis() - startCounter;
  44.          System.out.println("time for first iteration, ms: "+stopCounter);
  45.        }
  46.        long stopIt = System.currentTimeMillis() - startIt;
  47.        if (x%1000==0) System.out.println("iteration: "+x+" time: "+stopIt);
  48.      }
  49.  
  50.   if (sumBig.equals(BigInteger.valueOf(0)))
  51.      System.out.println(iIteration + " is prime!");
  52.   else {
  53.      String resString = sumBig.toString(16).toUpperCase();
  54.      int lenghtResString = resString.length();
  55.      if (lenghtResString>16) resString=resString.substring(lenghtResString-16,lenghtResString);
  56.      System.out.println(iIteration + " is not prime! Res64:" + resString);
  57.  
  58.      try (FileOutputStream results = new FileOutputStream("results.txt")) {
  59.           for (int k=0;k<=15;k++)
  60.             results.write(resString.charAt(k));
  61.        } catch (IOException e) {
  62.          System.out.println("Error to write file:" + e );
  63.        }
  64.   }
  65.   }
  66. /**
  67.  * Is it prime?
  68.  */
  69.     private static boolean isPrime(int N) {
  70.         if (N < 2) return false;
  71.         for (int i = 2; i*i <= N; i++)
  72.             if (N % i == 0) return false;
  73.         return true;
  74.     }
  75. }
RAW Paste Data