Programmatically solve this Vietnamese math problem for kids

2 points by marcmagn1 ↗ HN
So I found this Vietnamese math problem for kids: http://www.huffingtonpost.com/2015/05/20/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://pastebin.com/BZZ3EkGp , I would be curious to see yours.

7 comments

[ 5.7 ms ] story [ 32.7 ms ] thread
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.
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.
(comment deleted)
<?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"; } ?>