Advertisement
Guest User

(a / (b + c)) + (d / (e + f)) + (g / (h + i)) = 1

a guest
Feb 11th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.NoSuchElementException;
  3. import java.lang.reflect.Array;
  4. import java.util.ArrayList;
  5.  
  6. public class Permutations implements Iterator
  7. {
  8.  
  9.     private final int size;
  10.     private final Object[] elements;
  11.     private final Object output;
  12.     private final int[] permutation;
  13.     private static ArrayList<String> solutions = new ArrayList<>();
  14.  
  15.     private boolean next = true;
  16.  
  17.     public static boolean function(int[] a)
  18.     {
  19.         int[] b = new int[3];
  20.         b[0] = a[1] + a[2]; // add denominators
  21.         b[1] = a[4] + a[5]; // second set
  22.         b[2] = a[7] + a[8]; // third set
  23.         int d = b[0] * b[1] * b[2]; // multiply them together
  24.         int n = (a[0] * (d/b[0])) + (a[3] * (d/b[1])) + (a[6] * (d/b[2])); // get numerator to match common denominator
  25.         //System.out.println("Numerator" + n + " : Denominator:" + d);
  26.         if (n == d)
  27.         {
  28.             return true;
  29.         }
  30.         return false;
  31.  
  32.     }
  33.  
  34.     public static int[] stringArrayToIntArray(String[] s)
  35.     {
  36.         int[] r = new int[s.length];
  37.         for (int i = 0; i < s.length; i++)
  38.         {
  39.             r[i] = Integer.parseInt(s[i]);
  40.         }
  41.         return r;
  42.     }
  43.  
  44.  
  45.     public Permutations (Object [] e)
  46.     {
  47.       size = e.length;
  48.       elements = new Object [size];    // not suitable for primitives
  49.       System.arraycopy (e, 0, elements, 0, size);
  50.       output = Array.newInstance (e.getClass().getComponentType(), size);
  51.       System.arraycopy (e, 0, output, 0, size);
  52.       permutation = new int [size+1];
  53.       for (int i=0; i<size+1; i++) {
  54.          permutation [i]=i;
  55.       }
  56.     }
  57.  
  58.     private void formNextPermutation () {
  59.       for (int i=0; i<size; i++) {
  60.          // i+1 because perm[0] always = 0
  61.          // perm[]-1 because the numbers 1..size are being permuted
  62.          Array.set (output, i, elements[permutation[i+1]-1]);
  63.       }
  64.     }
  65.  
  66.     public boolean hasNext() {
  67.       return next;
  68.     }
  69.  
  70.     public void remove() throws UnsupportedOperationException {
  71.       throw new UnsupportedOperationException();
  72.     }
  73.  
  74.     private void swap (final int i, final int j) {
  75.       final int x = permutation[i];
  76.       permutation[i] = permutation [j];
  77.       permutation[j] = x;
  78.     }
  79.  
  80.     // does not throw NoSuchElement; it wraps around!
  81.     public Object next() throws NoSuchElementException {
  82.  
  83.       formNextPermutation ();  // copy original elements
  84.  
  85.       int i = size-1;
  86.       while (permutation[i]>permutation[i+1]) i--;
  87.  
  88.       if (i==0) {
  89.          next = false;
  90.          for (int j=0; j<size+1; j++) {
  91.             permutation [j]=j;
  92.          }
  93.          return output;
  94.       }
  95.  
  96.       int j = size;
  97.  
  98.       while (permutation[i]>permutation[j]) j--;
  99.       swap (i,j);
  100.       int r = size;
  101.       int s = i+1;
  102.       while (r>s) { swap(r,s); r--; s++; }
  103.  
  104.       return output;
  105.     }
  106.  
  107.     public String toString () {
  108.       final int n = Array.getLength(output);
  109.       final StringBuffer sb = new StringBuffer ("[");
  110.       for (int j=0; j<n; j++) {
  111.          sb.append (Array.get(output,j).toString());
  112.          if (j<n-1) sb.append (",");
  113.       }
  114.       sb.append("]");
  115.       return new String (sb);
  116.     }
  117.  
  118.     public static void main (String [] args) {
  119.        String[] p = {"1","2","3","4","5","6","7","8","9"};
  120.        int count = 0;
  121.  
  122.       for (Iterator i = new Permutations(p); i.hasNext(); ) {
  123.          final String [] a = (String []) i.next();
  124.          if (function(stringArrayToIntArray(a)))
  125.          {
  126.              solutions.add(i.toString());
  127.              count++;
  128.          }
  129.          System.out.println (i);
  130.  
  131.       }
  132.       System.out.println("Solutions:" + count);
  133.       for (String current : solutions)
  134.       {
  135.           System.out.println(current);
  136.       }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement