Minimum multiple of m where digital sum equals m: Difference between revisions

Content added Content deleted
(Added Forth solution)
(Added Prolog solution)
Line 794: Line 794:
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
17449 38269 56413 37037 1108909 142498 103507 154981 150661 1333333
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
163918 322579 315873 937342 1076923 1030303 880597 1469116 1157971 12842857
</pre>

=={{header|Prolog}}==
{{works with|SWI Prolog}}
<lang prolog>main:-
between(1, 40, N),
min_mult_dsum(N, M),
writef('%6r', [M]),
(0 is N mod 10 -> nl ; true),
fail.
main.

min_mult_dsum(N, M):-
min_mult_dsum(N, 1, M).

min_mult_dsum(N, M, M):-
P is M * N,
digit_sum(P, N),
!.
min_mult_dsum(N, K, M):-
L is K + 1,
min_mult_dsum(N, L, M).

digit_sum(N, Sum):-
digit_sum(N, Sum, 0).

digit_sum(N, Sum, S1):-
N < 10,
!,
Sum is S1 + N.
digit_sum(N, Sum, S1):-
divmod(N, 10, M, Digit),
S2 is S1 + Digit,
digit_sum(M, Sum, S2).</lang>

{{out}}
<pre>
1 1 1 1 1 1 1 1 1 19
19 4 19 19 13 28 28 11 46 199
19 109 73 37 199 73 37 271 172 1333
289 559 1303 847 1657 833 1027 1576 1282 17497
</pre>
</pre>