4 ms·
Haskell Solution: [(a,b,c,d,e,f) | a <- [39, 90, 75, 15, 57], b <- [9, 2, 58, 68, 48, 64], c <- [91, 29, 55, 16, 67, 8], d <- [22, 32, 25, 40, 54, 66], e <-
by psibi 13y ago
Haskell Solution:
[(a,b,c,d,e,f) | a <- [39, 90, 75, 15, 57], b <- [9, 2, 58, 68, 48, 64], c <- [91, 29, 55, 16, 67, 8], d <- [22, 32, 25, 40, 54, 66], e <- [41, 14, 30, 49, 01, 17], f <- [44, 63, 10, 83, 46, 03], a+b+c+d+e+f == 419 ]
- zeckalpha 13y agoThere is a hidden scrollbar there for me. This could go on multiple lines, too.
- pygy_ 13y agoWhat if you want to solve another matrix? Do you have to write another program?
- FreeFull 13y agoCould just take all the numbers as command-line arguments or something similar.
- StefanKarpinski 13y agoBoth this and the Mathematica solution linked below [1] are brute force, which strikes me as far less interesting than using linear programming. Of course, at this size of problem brute force works fine. You can write a brute force version easily in any language, including, of course, Julia: julia> function brute_force(P,t) for x1=P[1,:], x2=P[2,:], x3=P[3,:], x4=P[4,:], x5=P[5,:], x6=P[6,:] x1 + x2 + x3 + x4 + x5 + x6 == t || continue println("solution: $x1 $x2 $x3 $x4 $x5 $x6") end end brute_force (generic function with 1 method) julia> brute_force(P,419) solution: 90 68 91 66 41 63 solution: 90 48 91 66 41 83 solution: 90 64 67 66 49 83 solution: 88 68 91 40 49 83 The interesting thing about Iain's linear programming approach is that you can write the problem down the way it is defined and get a solution efficiently. [1] https://news.ycombinator.com/item?id=6425633 https://news.ycombinator.com/item?id=6425633