Guest User

Reddit Programming Prompt

a guest
Feb 10th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.92 KB | None | 0 0
  1.   /*
  2.  
  3.  (a / (b + c)) + (d / (e + f)) + (g / (h + i)) = 1
  4.  
  5. using only numbers 1 - 9, and only each number once!
  6.  
  7. When a solution is found, print the completed equation.
  8.  
  9.   */
  10.  
  11.    public class numProblem
  12.    {
  13.       static int tested, elim1, elim2, solutions,  recursions;
  14.      
  15.       public numProblem()
  16.       {
  17.       }
  18.      
  19.       public static void main(String[] args)
  20.       {
  21.       //loop from 1 to 9, running instances of solve
  22.          for(int x=1; x<10; x++)
  23.          {
  24.             System.out.println("Root Parameter: "+x);
  25.             solve(""+x);  
  26.          }
  27.          
  28.          int divides = elim1+ 2*(elim2)+3*(tested) ; //checks the number of divisions that occured
  29.        
  30.        
  31.          System.out.println("Recursions Executed: "+recursions);//how many times the method "solve" recursed
  32.          
  33.          System.out.println("Combinations Tested: "+tested); //how many 9 number combinations were tested for the solution
  34.          
  35.          System.out.println("1st Round "+elim1);     //how many recursions occured after the first test
  36.          
  37.          System.out.println("2nd Round: "+elim2);   //how many recursions occured after the second test
  38.      
  39.          System.out.println("Number of float divisions "+divides); 
  40.        
  41.          System.out.println("Number of Solutions: "+solutions);
  42.      
  43.       }
  44.    
  45.       public static void solve(String p) //p is the parameter that has been passed
  46.       {
  47.          float test=0; //variable used to check whether or not the current recurring pattern is possible
  48.          boolean newclean = true; //determines whether or not the new integer being added to the pattern has been used previously in the pattern
  49.          int prevlength = p.length(); //determine length
  50.          
  51.             //main solver
  52.          if(prevlength == 8) //if contains length is 8 (check solutions) -- if correct, then print
  53.          {
  54.             for(int a = 1; a< 10;  a++) //checking 1 through 9
  55.             {
  56.                newclean = true;
  57.                for(int b = 0; b< 8; b++) //checks to make sure a has not been previously used
  58.                {
  59.                   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)
  60.                   {
  61.                      newclean = false;
  62.                   }
  63.                }
  64.                if(newclean)//tests to see the result of the combination of integers
  65.                {
  66.                   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)) )   ) ;
  67.                   tested++;
  68.                }
  69.                if( test==1  && newclean) // condition to fulfill prompt requirements, causes a print
  70.                {
  71.                   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"   );
  72.                   solutions++;
  73.                }
  74.             }
  75.          }
  76.          //Eliminator 2
  77.          else if(prevlength == 5) //if length is 5 (loop going upwards, checking if total > 1 -- if not, then recurse)
  78.          {
  79.             for(int a = 1; a< 10;  a++) //checking 1 through 9
  80.             {
  81.                newclean = true;
  82.                for(int b = 0; b< 5; b++) //checks to make sure a has not been previously used
  83.                {
  84.                   if( a == Integer.parseInt(""+p.charAt(b)) )  // if a has not been used it will be added on to the string and solved for
  85.                   {
  86.                      newclean = false;
  87.                   }
  88.                }
  89.                if(newclean)
  90.                {
  91.                   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)))   ) ;
  92.                   elim2++;
  93.                }  
  94.                if(test>=1)
  95.                {
  96.                   break;
  97.                }
  98.                if(   test   < 1 && newclean) // condition to incur recursion, if this is not fulfilled, it is wasteful to calculate further possibilities
  99.                {
  100.                   solve(""+p+a);
  101.                   recursions++;
  102.                }
  103.             }  
  104.          }
  105.          //Eliminator 1
  106.          else if(prevlength == 2) //if length is 2 (loop going upwards, checking if total > 1 -- if not, then recurse)
  107.          {
  108.             for(int a = 1; a< 10;  a++) //checking 1 through 9
  109.             {
  110.                newclean = true;
  111.                for(int b = 0; b< 2; b++) //checks to make sure a has not been previously used
  112.                {
  113.                   if( a == Integer.parseInt(""+p.charAt(b)) ) // if a has not been used it will be added on to the string and solved for
  114.                   {
  115.                      newclean = false;
  116.                   }
  117.                }
  118.                if(newclean)
  119.                {
  120.                   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
  121.                   elim1++;
  122.                }
  123.                if(test>=1)//breaks when the tested value exceeds
  124.                {
  125.                   break;
  126.                }
  127.                if( (test < 1.0) && newclean ) // condition to incur recursion, if this is not fulfilled, it is wasteful to calculate further possibilities
  128.                {
  129.                   solve(""+p+a);
  130.                   recursions++;
  131.                }
  132.             }
  133.          }
  134.          //Main recursor for most checks
  135.          else //else if length is anything, recurse with the addition of all unused values
  136.          {
  137.          //char current = '0'; - temp line, may not be needed
  138.             for(int a = 1; a< 10;  a++) //checking 1 through 9
  139.             {
  140.                newclean = true;
  141.                for(int b = 0; b< prevlength; b++) //checks to make sure a has not been previously used
  142.                {
  143.                   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
  144.                   {
  145.                      newclean=false;
  146.                   }
  147.                }
  148.                if(newclean)
  149.                {
  150.                   solve(""+p+a);
  151.                   recursions++;
  152.                }
  153.             }
  154.          }
  155.       }
  156.    }
Add Comment
Please, Sign In to add comment