Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- public class Permutations implements Iterator
- {
- private final int size;
- private final Object[] elements;
- private final Object output;
- private final int[] permutation;
- private static ArrayList<String> solutions = new ArrayList<>();
- private boolean next = true;
- public static boolean function(int[] a)
- {
- int[] b = new int[3];
- b[0] = a[1] + a[2]; // add denominators
- b[1] = a[4] + a[5]; // second set
- b[2] = a[7] + a[8]; // third set
- int d = b[0] * b[1] * b[2]; // multiply them together
- int n = (a[0] * (d/b[0])) + (a[3] * (d/b[1])) + (a[6] * (d/b[2])); // get numerator to match common denominator
- //System.out.println("Numerator" + n + " : Denominator:" + d);
- if (n == d)
- {
- return true;
- }
- return false;
- }
- public static int[] stringArrayToIntArray(String[] s)
- {
- int[] r = new int[s.length];
- for (int i = 0; i < s.length; i++)
- {
- r[i] = Integer.parseInt(s[i]);
- }
- return r;
- }
- public Permutations (Object [] e)
- {
- size = e.length;
- elements = new Object [size]; // not suitable for primitives
- System.arraycopy (e, 0, elements, 0, size);
- output = Array.newInstance (e.getClass().getComponentType(), size);
- System.arraycopy (e, 0, output, 0, size);
- permutation = new int [size+1];
- for (int i=0; i<size+1; i++) {
- permutation [i]=i;
- }
- }
- private void formNextPermutation () {
- for (int i=0; i<size; i++) {
- // i+1 because perm[0] always = 0
- // perm[]-1 because the numbers 1..size are being permuted
- Array.set (output, i, elements[permutation[i+1]-1]);
- }
- }
- public boolean hasNext() {
- return next;
- }
- public void remove() throws UnsupportedOperationException {
- throw new UnsupportedOperationException();
- }
- private void swap (final int i, final int j) {
- final int x = permutation[i];
- permutation[i] = permutation [j];
- permutation[j] = x;
- }
- // does not throw NoSuchElement; it wraps around!
- public Object next() throws NoSuchElementException {
- formNextPermutation (); // copy original elements
- int i = size-1;
- while (permutation[i]>permutation[i+1]) i--;
- if (i==0) {
- next = false;
- for (int j=0; j<size+1; j++) {
- permutation [j]=j;
- }
- return output;
- }
- int j = size;
- while (permutation[i]>permutation[j]) j--;
- swap (i,j);
- int r = size;
- int s = i+1;
- while (r>s) { swap(r,s); r--; s++; }
- return output;
- }
- public String toString () {
- final int n = Array.getLength(output);
- final StringBuffer sb = new StringBuffer ("[");
- for (int j=0; j<n; j++) {
- sb.append (Array.get(output,j).toString());
- if (j<n-1) sb.append (",");
- }
- sb.append("]");
- return new String (sb);
- }
- public static void main (String [] args) {
- String[] p = {"1","2","3","4","5","6","7","8","9"};
- int count = 0;
- for (Iterator i = new Permutations(p); i.hasNext(); ) {
- final String [] a = (String []) i.next();
- if (function(stringArrayToIntArray(a)))
- {
- solutions.add(i.toString());
- count++;
- }
- System.out.println (i);
- }
- System.out.println("Solutions:" + count);
- for (String current : solutions)
- {
- System.out.println(current);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement