I came across this puzzle, to fill in the blanks so that it solved the math.

        _ _ 7 _
        x _ 7 _
    ----------- (x means multiply, it is not a variable)
      _ _ _ _ _
    _ _ _ 2 _ 0
    8 _ 5 _ 0 0
    ----------- total
    _ _ _ _ _ _

So I created a PHP script to brute force it.
I gave it limited parameters and told it to run.
The only problem is it has been running for 2 days without luck. (i think there are like 2 trillion possibilities)

So I am wondering if anyone can improve this script.
Thanks!

    <?php
    set_time_limit(0);

$start = (float) array_sum(explode(' ', microtime()));

for ($a = 1; $a <= 9; $a++) {
 for ($b = 0; $b <= 9; $b++) {
  for ($c = 0; $c <= 9; $c++) {
   for ($d = 1; $d <= 9; $d++) {
    for ($e = 0; $e <= 9; $e++) {
     for ($f = 1; $f <= 9; $f++) {
      for ($g = 0; $g <= 9; $g++) {
       for ($h = 0; $h <= 9; $h++) {
        for ($i = 0; $i <= 9; $i++) {
         for ($j = 0; $j <= 9; $j++) {
          for ($k = 0; $k <= 9; $k++) { 
           for ($L = 0; $L <= 9; $L++) {
            for ($m = 0; $m <= 9; $m++) {
             for ($n = 0; $n <= 9; $n++) {
              for ($o = 0; $o <= 9; $o++) {
               for ($p = 0; $p <= 9; $p++) {
                for ($q = 0; $q <= 9; $q++) {
                 $line1 =           $a . $b . "7" . $c;
                 $line2 =                $d . "7" . $e;
                 //       ----------------------------
                 $line3 =      $f . $g . $h .  $i . $j;
                 $line4 = $k . $L . $m . "2" . $o . "0";
                 $line5 = "8". $p . "5". $q .  "0". "0";
                 $addtotal = $line3 + $line4 + $line5;               
                 $multotal = $line1 * $line2;
                 if($addtotal == $multotal){
                  echo $line1 . "\r\n";
                  echo $line2 . "\r\n";
                  echo "----------------------------\r\n";
                  echo $line3 . "\r\n";
                  echo $line4 . "\r\n";
                  echo $line5 . "\r\n";
                  echo "----------------------------\r\n\r\n";
                  echo $multotal;
                  $end = (float) array_sum(explode(' ', microtime()));
                  $perc = ($end-start);
                  $perc = ($perc / 60);
                  echo "Proccessing time: " . sprintf("%.4f", ($perc)) . "mins";
                  exit;
                 }
                }
               }
              }
             }
            }
           }
          }
         }
        }
       }
      }
     }
    }
   }
  }
 }
}
?>

    Fifteen seconds' perusal: you're doing things like setting $c=0 and $e=0 and $j=3 .... and that's not going to work.

    You only need to vary the first five digits - all the others are derived from those.

    Vary the first five digits and use those to fill in the digits in the next two rows, trying to find a set that will make 2, 8, and 5 appear in the right locations. Once you've found such a set, go on to complete the last row.

      I understand what you mean by only having to loop thru a-e. but i dont know how to tackle the rest

      so should i change line 3

      $line3 = $e * $a . $b . "7" . $c;

      But I dont know how to make line 4 and 5

        another attempt failed attempt.

        for some reason it is ignoring the line4 string length
        it is always creating a 4 character line 4. and it should be 5 characters

        <?php
        set_time_limit(0);
        
        $start = (float) array_sum(explode(' ', microtime()));
        
        for ($a = 1; $a <= 9; $a++) {
         for ($b = 0; $b <= 9; $b++) {
          for ($c = 0; $c <= 9; $c++) {
           for ($d = 1; $d <= 9; $d++) {
            for ($e = 1; $e <= 9; $e++) {
                         $line1 =           $a . $b . "7" . $c;
                         $line2 =                $d . "7" . $e;
        		 $line3 = $a . $b . "7" . $c;
        		 $line3 = $e * $line3;
        		 $line4 =  $a . $b . "7" . $c;
        		 $line4 = 7 * $line4;
        		 $line4 = $line4 . "0";
        		 $line5 = $a . $b . "7" . $c;
        		 $line5 = $d * $line5;
        		 $line5 = $line5 . "00";
        
        	 if(strlen($line4) !=5){
        	  continue;
        	 }
        
        	 if(strlen($line5) !=6){
        	  continue;
        	 }
        
        	 if(strlen($line6) !=6){
        	  continue;
        	 }
        
        	 if(substr($line4, 3, 1) != 2){
        	  continue;
        	 }
        
        	 if(substr($line5, 0, 1) != 8){
        	  continue;
        	 }
        
        	 if(substr($line5, 2, 1) !=5){
        	  continue;
        	 }
        
                     $addtotal = $line3 + $line4 + $line5;               
                     $multotal = $line1 * $line2;
        
                     if($addtotal == $multotal){
                      echo $line1 . "\r\n";
                      echo $line2 . "\r\n";
                      echo "----------------------------\r\n";
                      echo $line3 . "\r\n";
                      echo $line4 . "\r\n";
                      echo $line5 . "\r\n";
                      echo "----------------------------\r\n\r\n";
                      echo $multotal. "\r\n";
                      $end = (float) array_sum(explode(' ', microtime()));
                      $perc = ($end-start);
                      $perc = ($perc / 60);
                      echo "Proccessing time: " . sprintf("%.4f", ($perc)) . " mins";
                      exit;
                     }
        }
           }
          }
         }
        }
        ?>
        

          Well, you know how long multiplication works, of course. The third row is

          $third_row = "$a$b7$c" * $e;
          

          And similarly for the fourth and fifth rows (check the appropriate digits). Then sum.

            I think I just had some code off

            this seems to work

            <?php
            set_time_limit(0);
            
            function microtime_float()
            {
                list($usec, $sec) = explode(" ", microtime());
                return ((float)$usec + (float)$sec);
            }
            
            $time_start = microtime_float();
            
            for ($a = 0; $a <= 9; $a++) {
            for ($b = 0; $b <= 9; $b++) {
              for ($c = 0; $c <= 9; $c++) {
               for ($d = 0; $d <= 9; $d++) {
                for ($e = 0; $e <= 9; $e++) { 
                             $line1 = $a . $b . "7" . $c;
                             $line2 = $d . "7" . $e;
            		 $line3 = $a . $b . "7" . $c;
            		 $line3 = $e * $line3;
            		 $line4 =  $a . $b . "7" . $c;
            		 $line4 = 7 * $line4;
            		 $line4 = $line4 . "0";
            		 $line5 = $a . $b . "7" . $c;
            		 $line5 = $d * $line5;
            		 $line5 = $line5 . "00";
            
            
                 if(strlen($line3) !=5){
                  continue;
                 }
            
                 if(strlen($line4) !=6){
                  continue;
                 }
            
                 if(strlen($line5) !=6){
                  continue;
                 }
            
                 if(substr($line4, 3, 1) != 2){
                  continue;
                 }
            
                 if(substr($line5, 0, 1) != 8){
                  continue;
                 }
            
                 if(substr($line5, 2, 1) !=5){
                  continue;
                 } 
            
            
                         $addtotal = $line3 + $line4 + $line5;               
                         $multotal = $line1 * $line2;
            
            
            
                         if($addtotal == $multotal){
                          echo $line1 . "\r\n";
                          echo $line2 . "\r\n";
                          echo "----------------------------\r\n";
                          echo $line3 . "\r\n";
                          echo $line4 . "\r\n";
                          echo $line5 . "\r\n";
                          echo "----------------------------\r\n";
                          echo $multotal. "\r\n";
            	  $time_end = microtime_float();
            	  $time_stopped = $time_end - $time_start;
            	  echo "Total time: " . $time_stopped . " seconds\r\n";
                          exit;
                         }
            
            }
               }
              }
             }
            }
            ?>

            1475

            677

            10325
            103250

            885000

            998575
            Total time: 0.070544004440308 seconds

              Write a Reply...