/* Tallys' division puzzle in Picat. https://www.facebook.com/tallys/posts/10227556965777663 """ A student sent me this puzzle yesterday. I urge all my former students to set up an optimization model in Excel to solve it not just for 9, but for 8, 7, 6, etc. Then go impress your friends at parties. #optimization #modeling #math #puzzles COOL FOLLOW-UP QUESTION: Is the solution with ratio 9 unique? (just found out the answer to this) Arrange the digits 0..9 in fractional form so that xx xxx ------ = 9 xx xxx """ Here are the 94 possible solutions: 26970 / 13485 = 2 27096 / 13548 = 2 27690 / 13845 = 2 29076 / 14538 = 2 29370 / 14685 = 2 29670 / 14835 = 2 29706 / 14853 = 2 29730 / 14865 = 2 30972 / 15486 = 2 32970 / 16485 = 2 37092 / 18546 = 2 37290 / 18645 = 2 41358 / 20679 = 2 41538 / 20769 = 2 41586 / 20793 = 2 46158 / 23079 = 2 53418 / 26709 = 2 53814 / 26907 = 2 54138 / 27069 = 2 54186 / 27093 = 2 54618 / 27309 = 2 58134 / 29067 = 2 58146 / 29073 = 2 58614 / 29307 = 2 61458 / 30729 = 2 61584 / 30792 = 2 61854 / 30927 = 2 62970 / 31485 = 2 64158 / 32079 = 2 65418 / 32709 = 2 65814 / 32907 = 2 69702 / 34851 = 2 70296 / 35148 = 2 70962 / 35481 = 2 76290 / 38145 = 2 76902 / 38451 = 2 90276 / 45138 = 2 90372 / 45186 = 2 90762 / 45381 = 2 92370 / 46185 = 2 93702 / 46851 = 2 96270 / 48135 = 2 96702 / 48351 = 2 97026 / 48513 = 2 97032 / 48516 = 2 97062 / 48531 = 2 97230 / 48615 = 2 97302 / 48651 = 2 50382 / 16794 = 3 53082 / 17694 = 3 61749 / 20583 = 3 69174 / 23058 = 3 91746 / 30582 = 3 96174 / 32058 = 3 60948 / 15237 = 4 68940 / 17235 = 4 69408 / 17352 = 4 81576 / 20394 = 4 81756 / 20439 = 4 86940 / 21735 = 4 94068 / 23517 = 4 94860 / 23715 = 4 67290 / 13458 = 5 67920 / 13584 = 5 69270 / 13854 = 5 72690 / 14538 = 5 72930 / 14586 = 5 73290 / 14658 = 5 76920 / 15384 = 5 79230 / 15846 = 5 79320 / 15864 = 5 92670 / 18534 = 5 92730 / 18546 = 5 93270 / 18654 = 5 98532 / 14076 = 7 83672 / 10459 = 8 83752 / 10469 = 8 84296 / 10537 = 8 84632 / 10579 = 8 84736 / 10592 = 8 85392 / 10674 = 8 85432 / 10679 = 8 85936 / 10742 = 8 86352 / 10794 = 8 87456 / 10932 = 8 87536 / 10942 = 8 87624 / 10953 = 8 87632 / 10954 = 8 96584 / 12073 = 8 98456 / 12307 = 8 98760 / 12345 = 8 95742 / 10638 = 9 95823 / 10647 = 9 97524 / 10836 = 9 len = 94 If we also allow leading 0 then there are 281 solutions, e.g. 13458 / 06729 = 2 13584 / 06792 = 2 13854 / 06927 = 2 14538 / 07269 = 2 14586 / 07293 = 2 14658 / 07329 = 2 ... 15768 / 03942 = 4 17568 / 04392 = 4 23184 / 05796 = 4 ... 57429 / 06381 = 9 58239 / 06471 = 9 75249 / 08361 = 9 45792 / 03816 = 12 73548 / 06129 = 12 89532 / 07461 = 12 ... 93126 / 05478 = 17 28674 / 01593 = 18 51984 / 02736 = 19 81567 / 04293 = 19 ... 51678 / 02349 = 22 36294 / 01578 = 23 81627 / 03549 = 23 81972 / 03564 = 23 ... 37584 / 01296 = 29 73689 / 02541 = 29 75168 / 02349 = 32 48265 / 01379 = 35 ... 94736 / 01528 = 62 83754 / 01269 = 66 98736 / 01452 = 68 len = 281 This model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. % % Basic problem: % .. #= 9 % and no leading 0s. % go ?=> L = [A,B,C,D,E,F,G,H,I,J], L :: 0..9, A #> 0, F #> 0, all_different(L), % this is integer division which gives spurious solutions % (A*10000 + B*1000 + C*100 + D*10 + E) / (F*10000 + G*1000 + H*100 + I*10 + J) #= 9, (A*10000 + B*1000 + C*100 + D*10 + E) #= 9 * (F*10000 + G*1000 + H*100 + I*10 + J), solve(L), println(L), println((A*10000 + B*1000 + C*100 + D*10 + E) / (F*10000 + G*1000 + H*100 + I*10 + J)), nl, fail, nl. go => true. % % Check all results. % go2 ?=> AllowLeadingZero = true, % AllowLeadingZero = false, % All = find_all(Str, (member(Res,1..200000),tallys_puzzle(Res,AllowLeadingZero,Str))), All = find_all(Str, (Res::1..2_000_000,indomain(Res),tallys_puzzle(Res,AllowLeadingZero,Str))), % println(All), foreach(A in All) println(A) end, println(len=All.len), nl. go2 => true. tallys_puzzle(Res,AllowLeadingZero,Str) => L = [A,B,C,D,E,F,G,H,I,J], L :: 0..9, if not AllowLeadingZero then A #> 0, F #> 0 end, all_different(L), (A*10000 + B*1000 + C*100 + D*10 + E) #= Res * (F*10000 + G*1000 + H*100 + I*10 + J), solve($[ff,split],L), (A*10000 + B*1000 + C*100 + D*10 + E) / (F*10000 + G*1000 + H*100 + I*10 + J) == 1.0*Res, X = to_fstring("%w%w%w%w%w", A,B,C,D,E), Y = to_fstring("%w%w%w%w%w", F,G,H,I,J), Str = to_fstring("%s / %s = %w",X, Y, Res).