

Programmatically solve this Vietnamese math problem for kids - marcmagn1

So I found this Vietnamese math problem for kids: http:&#x2F;&#x2F;www.huffingtonpost.com&#x2F;2015&#x2F;05&#x2F;20&#x2F;vietnamese-math-puzzle_n_7346332.html and tried to find a way to solve it programmatically. I am not a developer just a python beginner. Here is my solution: http:&#x2F;&#x2F;pastebin.com&#x2F;BZZ3EkGp , I would be curious to see yours.
======
Jagadeesh1210
<?php while(1) { $numbers = '123456789'; $numStringLength = strlen($numbers);
$num1=$numbers[rand(0, $numStringLength - 1)];

$numbers=str_replace($num1,'',$numbers); $numStringLength = strlen($numbers);
$num2=$numbers[rand(0, $numStringLength - 1)];

$numbers=str_replace($num2,'',$numbers); $numStringLength = strlen($numbers);
$num3=$numbers[rand(0, $numStringLength - 1)];

$numbers=str_replace($num3,'',$numbers); $numStringLength = strlen($numbers);
$num4=$numbers[rand(0, $numStringLength - 1)];

$numbers=str_replace($num4,'',$numbers); $numStringLength = strlen($numbers);
$num5=$numbers[rand(0, $numStringLength - 1)];

$numbers=str_replace($num5,'',$numbers); $numStringLength = strlen($numbers);
$num6=$numbers[rand(0, $numStringLength - 1)];

$numbers=str_replace($num6,'',$numbers); $numStringLength = strlen($numbers);
$num7=$numbers[rand(0, $numStringLength - 1)];

$numbers=str_replace($num7,'',$numbers); $numStringLength = strlen($numbers);
$num8=$numbers[rand(0, $numStringLength - 1)];

$numbers=str_replace($num8,'',$numbers); $numStringLength = strlen($numbers);
$num9=$numbers[rand(0, $numStringLength - 1)];

$sum=$num1+13 _$num2 /$num3+$num4+12_$num5-$num6-11+$num7*$num8/$num9-10;

if($sum == 66) echo "Math Puzzle Answer
String::".$num1.$num2.$num3.$num4.$num5.$num6.$num7.$num8.$num9."||Sum::".$sum."\n";
} ?>

------
Someone
Not what you ask for, and I didn't completely solve it when I saw it a few
days ago, but I think it's reasonably solvable by hand. I would first make the
search space a lot smaller by assuming that b/c and gh/i are integers (hm, I
think they must be, as you cannot end up with an integer by adding just two
fractions that have different integer denominators)

And of course, simplify -11 ... -10 to -21. Then, taking out a factor of 12
might help:

    
    
      a + 13 * b / c + d + 12 * e - f - 11 + g * h / i - 10
      = 12 * (b/c + e - 2) + (a + b/c + d - f + g * h / i + 3)
      = 12 * x + y
    

Now I would guess that y is positive (may be not too hard to prove) and x
equals 4 or 5. If that leads to a dead end, work your way down for values of
x.

~~~
sharth
1/2 + 4/8 == an integer.

~~~
Someone
Thanks.

------
sharth
So we can actually iterate over every possible solution pretty quickly. If I
cared more, I'd look into using `fractions.Fraction` so that I didn't need to
use floating point math.

    
    
        from __future__ import division
        import itertools
        
        def f(a, b, c, d, e, f, g, h, i):
            return a + 13 * b / c + d + 12 * e - f - 11 + g * h / i - 10
        
        for choices in itertools.permutations(range(1, 10), 9):
            if abs(f(*choices) - 66) < 0.001:
                print choices
    

There are also some solution that your code will give that are wrong due to
your code using integer division.

------
duncan_bayne
[https://github.com/duncan-bayne/viet-cl/blob/master/viet-
cl....](https://github.com/duncan-bayne/viet-cl/blob/master/viet-cl.lisp)

( I'm seeking feedback here:
[http://codereview.stackexchange.com/questions/91444/solving-...](http://codereview.stackexchange.com/questions/91444/solving-
the-vietnamese-school-maths-problem) )

