Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- (a / (b + c)) + (d / (e + f)) + (g / (h + i)) = 1
- using only numbers 1 - 9, and only each number once!
- When a solution is found, print the completed equation.
- */
- public class numProblem
- {
- static int tested, elim1, elim2, solutions, recursions;
- public numProblem()
- {
- }
- public static void main(String[] args)
- {
- //loop from 1 to 9, running instances of solve
- for(int x=1; x<10; x++)
- {
- System.out.println("Root Parameter: "+x);
- solve(""+x);
- }
- int divides = elim1+ 2*(elim2)+3*(tested) ; //checks the number of divisions that occured
- System.out.println("Recursions Executed: "+recursions);//how many times the method "solve" recursed
- System.out.println("Combinations Tested: "+tested); //how many 9 number combinations were tested for the solution
- System.out.println("1st Round "+elim1); //how many recursions occured after the first test
- System.out.println("2nd Round: "+elim2); //how many recursions occured after the second test
- System.out.println("Number of float divisions "+divides);
- System.out.println("Number of Solutions: "+solutions);
- }
- public static void solve(String p) //p is the parameter that has been passed
- {
- float test=0; //variable used to check whether or not the current recurring pattern is possible
- boolean newclean = true; //determines whether or not the new integer being added to the pattern has been used previously in the pattern
- int prevlength = p.length(); //determine length
- //main solver
- if(prevlength == 8) //if contains length is 8 (check solutions) -- if correct, then print
- {
- for(int a = 1; a< 10; a++) //checking 1 through 9
- {
- newclean = true;
- for(int b = 0; b< 8; b++) //checks to make sure a has not been previously used
- {
- if( a == Integer.parseInt(""+p.charAt(b)) ) // if a has been used newclean is set to false, so the solution wont be tested. (having repeated numbers would make it an invalid solution)
- {
- newclean = false;
- }
- }
- if(newclean)//tests to see the result of the combination of integers
- {
- test = ( Integer.parseInt(""+p.charAt(2)) / (float)( Integer.parseInt(""+p.charAt(0)) + Integer.parseInt(""+p.charAt(1)) ) ) + ( Integer.parseInt(""+p.charAt(5)) / (float)( Integer.parseInt(""+p.charAt(3)) +Integer.parseInt(""+p.charAt(4)) ) ) + ( a / (float)( Integer.parseInt(""+p.charAt(6)) +Integer.parseInt(""+p.charAt(7)) ) ) ;
- tested++;
- }
- if( test==1 && newclean) // condition to fulfill prompt requirements, causes a print
- {
- System.out.println("Solution: ( " +Integer.parseInt(""+p.charAt(2))+" / ("+Integer.parseInt(""+p.charAt(0))+" + "+Integer.parseInt(""+p.charAt(1))+") ) +( "+Integer.parseInt(""+p.charAt(5))+" / ("+Integer.parseInt(""+p.charAt(3))+" + "+Integer.parseInt(""+p.charAt(4))+") ) + ( " +a+" / ("+Integer.parseInt(""+p.charAt(6))+" + "+Integer.parseInt(""+p.charAt(7))+") ) = 1" );
- solutions++;
- }
- }
- }
- //Eliminator 2
- else if(prevlength == 5) //if length is 5 (loop going upwards, checking if total > 1 -- if not, then recurse)
- {
- for(int a = 1; a< 10; a++) //checking 1 through 9
- {
- newclean = true;
- for(int b = 0; b< 5; b++) //checks to make sure a has not been previously used
- {
- if( a == Integer.parseInt(""+p.charAt(b)) ) // if a has not been used it will be added on to the string and solved for
- {
- newclean = false;
- }
- }
- if(newclean)
- {
- test = ( Integer.parseInt(""+p.charAt(2)) / (float)( Integer.parseInt(""+p.charAt(0)) + Integer.parseInt(""+p.charAt(1)) ) ) + ( a / (float)(Integer.parseInt(""+p.charAt(3)) +Integer.parseInt(""+p.charAt(4))) ) ;
- elim2++;
- }
- if(test>=1)
- {
- break;
- }
- if( test < 1 && newclean) // condition to incur recursion, if this is not fulfilled, it is wasteful to calculate further possibilities
- {
- solve(""+p+a);
- recursions++;
- }
- }
- }
- //Eliminator 1
- else if(prevlength == 2) //if length is 2 (loop going upwards, checking if total > 1 -- if not, then recurse)
- {
- for(int a = 1; a< 10; a++) //checking 1 through 9
- {
- newclean = true;
- for(int b = 0; b< 2; b++) //checks to make sure a has not been previously used
- {
- if( a == Integer.parseInt(""+p.charAt(b)) ) // if a has not been used it will be added on to the string and solved for
- {
- newclean = false;
- }
- }
- if(newclean)
- {
- test = a / (float)( Integer.parseInt(""+p.charAt(0)) + Integer.parseInt(""+p.charAt(1)) ) ; //Tests the a, b, and c variables to see if they equal
- elim1++;
- }
- if(test>=1)//breaks when the tested value exceeds
- {
- break;
- }
- if( (test < 1.0) && newclean ) // condition to incur recursion, if this is not fulfilled, it is wasteful to calculate further possibilities
- {
- solve(""+p+a);
- recursions++;
- }
- }
- }
- //Main recursor for most checks
- else //else if length is anything, recurse with the addition of all unused values
- {
- //char current = '0'; - temp line, may not be needed
- for(int a = 1; a< 10; a++) //checking 1 through 9
- {
- newclean = true;
- for(int b = 0; b< prevlength; b++) //checks to make sure a has not been previously used
- {
- if(a == Integer.parseInt(""+p.charAt(b))) // if a has not been used it will be added on to the string and the string will be used as the new parameter
- {
- newclean=false;
- }
- }
- if(newclean)
- {
- solve(""+p+a);
- recursions++;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment