Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.mersenne;
- import java.util.Scanner;
- import java.math.BigInteger;
- import java.io.*;
- /**
- * Java implementation of LL.
- **/
- class SimpleLucas {
- public static void main(String args[]) {
- BigInteger sumBig = BigInteger.valueOf(4);
- System.out.println("Enter exponent: ");
- Scanner sc = new Scanner(System.in);
- int iIteration = sc.nextInt();
- System.out.print("Start calculating ... ");
- if (isPrime(iIteration)==false) {
- System.out.println("Exponent is not prime!");
- return;
- }
- System.out.println("Your exponent "+iIteration+" is prime!");
- BigInteger bi1, bi2, MB, minusTwo;
- bi1 = new BigInteger("2");
- bi2 = bi1.pow(iIteration);
- MB = bi2.add(BigInteger.valueOf(-1));
- minusTwo = new BigInteger("-2");
- long startCounter = System.currentTimeMillis();
- BigInteger powBig;
- System.out.println("Variables is init");
- for (int x=1;x<=iIteration-2;x++) {
- long startIt = System.currentTimeMillis();
- powBig = sumBig.pow(2);
- powBig = powBig.add(minusTwo);
- sumBig = powBig.mod(MB);
- if (x==1){
- long stopCounter = System.currentTimeMillis() - startCounter;
- System.out.println("time for first iteration, ms: "+stopCounter);
- }
- long stopIt = System.currentTimeMillis() - startIt;
- if (x%1000==0) System.out.println("iteration: "+x+" time: "+stopIt);
- }
- if (sumBig.equals(BigInteger.valueOf(0)))
- System.out.println(iIteration + " is prime!");
- else {
- String resString = sumBig.toString(16).toUpperCase();
- int lenghtResString = resString.length();
- if (lenghtResString>16) resString=resString.substring(lenghtResString-16,lenghtResString);
- System.out.println(iIteration + " is not prime! Res64:" + resString);
- try (FileOutputStream results = new FileOutputStream("results.txt")) {
- for (int k=0;k<=15;k++)
- results.write(resString.charAt(k));
- } catch (IOException e) {
- System.out.println("Error to write file:" + e );
- }
- }
- }
- /**
- * Is it prime?
- */
- private static boolean isPrime(int N) {
- if (N < 2) return false;
- for (int i = 2; i*i <= N; i++)
- if (N % i == 0) return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement