Advertisement
Guest User

Untitled

a guest
Jul 1st, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. /******************************************************************************
  2.                            Code by AngelAtheist
  3.          Run with https://www.onlinegdb.com/online_java_compiler
  4. *******************************************************************************/
  5.  
  6. import java.math.BigInteger;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. public class Main
  11. {
  12.     public static void main(String[] args) {
  13.                 Long startTime = System.currentTimeMillis();
  14.         List<BigInteger> fractions = new ArrayList<BigInteger>();
  15.         //fractions.add(new BigInteger("17"));
  16.         fractions.add(new BigInteger("19"));
  17.         fractions.add(new BigInteger("23"));
  18.         fractions.add(new BigInteger("29"));
  19.         fractions.add(new BigInteger("31"));
  20.         // 34
  21.         fractions.add(new BigInteger("37"));
  22.         // 38
  23.         fractions.add(new BigInteger("41"));
  24.         fractions.add(new BigInteger("43"));
  25.         // 46
  26.         fractions.add(new BigInteger("47"));
  27.         // 51
  28.         fractions.add(new BigInteger("53"));
  29.         // 57
  30.         // 58
  31.         fractions.add(new BigInteger("59"));
  32.         fractions.add(new BigInteger("61"));
  33.         // 62
  34.         BigInteger largePrimeProduct = new BigInteger("1");
  35.         for (BigInteger fraction : fractions) {
  36.             largePrimeProduct = largePrimeProduct.multiply(fraction);
  37.         }
  38.         BigInteger denominator = new BigInteger("1");
  39.         for (int i = 2; i <= 64; i++) {
  40.             BigInteger factor = new BigInteger(String.valueOf(i));
  41.             BigInteger gcd = denominator.gcd(factor);
  42.             denominator = denominator.multiply(factor).divide(gcd);
  43.         }
  44.         System.out.println("full denominator is " + denominator.toString());
  45.         //fractions.add(5, new BigInteger("34")); // 17*2
  46.         fractions.add(5, new BigInteger("38")); // 19*2
  47.         fractions.add(8, new BigInteger("46")); // 23*2
  48.         //fractions.add(12, new BigInteger("51")); // 17*3
  49.         fractions.add(11, new BigInteger("57")); // 19*3
  50.         fractions.add(12, new BigInteger("58")); // 29*2
  51.         fractions.add(new BigInteger("62")); // 31*2
  52.         List<BigInteger> partials = new ArrayList<BigInteger>();
  53.         for (BigInteger fraction : fractions) {
  54.             BigInteger partial = denominator.divide(fraction);
  55.             partial = partial.mod(largePrimeProduct);
  56.             partials.add(partial);
  57.         }
  58.  
  59.         BigInteger lowestMatch = largePrimeProduct;
  60.  
  61.         for (int bits = 0; bits < Math.pow(3, fractions.size()); bits++) {
  62.             BigInteger sum = BigInteger.ZERO;
  63.             for (int index = 0; index < fractions.size(); index++) {
  64.                 int bitVal = (bits / (int)Math.pow(3, index)) % 3;
  65.                 if (bitVal < 0) {
  66.                     bitVal = bitVal + 3;
  67.                 }
  68.                 if (bitVal == 2) {
  69.                     sum = sum.add(partials.get(index));
  70.                 } else if (bitVal == 1) {
  71.                     // do nothing
  72.                 } else {
  73.                     sum = sum.subtract(partials.get(index));
  74.                 }
  75.             }
  76.             BigInteger sumRes = sum.mod(largePrimeProduct);
  77.             if (sumRes.compareTo(lowestMatch) != 1 && !sumRes.equals(BigInteger.ZERO)) {
  78.                 // repeat the bit computation, getting the fraction values for printing purposes
  79.                 String printString = "sum of " + sumRes + " found with ";
  80.                 for (int index = 0; index < fractions.size(); index++) {
  81.                     int bitVal = (bits / (int)Math.pow(3, index)) % 3;
  82.                     if (bitVal < 0) {
  83.                         bitVal = bitVal + 3;
  84.                     }
  85.                     if (bitVal == 2) {
  86.                         printString = printString + "+" + fractions.get(index) + ",";
  87.                     } else if (bitVal == 1) {
  88.                         printString = printString + "0" + fractions.get(index) + ",";
  89.                     } else {
  90.                         printString = printString + "-" + fractions.get(index) + ",";
  91.                     }
  92.                 }
  93.                 System.out.println(printString);
  94.                 lowestMatch = sumRes;
  95.             }
  96.         }
  97.         BigInteger fractionInverse = denominator.divide(lowestMatch);
  98.         Long endTime = System.currentTimeMillis();
  99.         Long duration = (endTime - startTime) / 1000;
  100.         Long durationSeconds = duration % 60;
  101.         if (durationSeconds < 0) {
  102.             durationSeconds += 60;
  103.         }
  104.         Long durationMinutes = duration / 60;
  105.         System.out.println("ran and found lower bound of approximately 1/" + fractionInverse);
  106.         System.out.println("it took " + durationMinutes + " minutes and " + durationSeconds + " seconds");
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement