Digital root/Multiplicative digital root: Difference between revisions
Content added Content deleted
(→{{header|Ruby}}: Use Ruby 2.4 method "digits") |
(Added a picolisp solution.) |
||
Line 2,020: | Line 2,020: | ||
8: [8, 18, 24, 29, 36] |
8: [8, 18, 24, 29, 36] |
||
9: [9, 19, 33, 91, 119]</pre> |
9: [9, 19, 33, 91, 119]</pre> |
||
=={{header|PicoLisp}}== |
|||
<lang picolisp>(de mdr-mp (N) |
|||
"Returns the solutions in a list, i.e., '(MDR MP)" |
|||
(let MP 0 |
|||
(while (< 1 (length N)) |
|||
(setq N (apply * (mapcar format (chop N)))) |
|||
(inc 'MP) ) |
|||
(list N MP) ) ) |
|||
# Get the MDR/MP of these nums. |
|||
(setq Test-nums '(123321 7739 893 899998)) |
|||
(let Fmt (6 5 5) |
|||
(tab Fmt "Values" "MDR" "MP") |
|||
(tab Fmt "======" "===" "==") |
|||
(for I Test-nums |
|||
(let MDR-MP (mdr-mp I) |
|||
(tab Fmt I (car MDR-MP) (cadr MDR-MP)) ) ) ) |
|||
(prinl) |
|||
# Get the nums of these MDRs. |
|||
(setq *Want 5) |
|||
(setq *Solutions (make (for MDR (range 0 9) |
|||
(link (make (let N 0 (until (= *Want (length (made))) |
|||
(when (= MDR (car (mdr-mp N))) |
|||
(link N) ) |
|||
(inc 'N) )))) ))) |
|||
(let Fmt (3 1 -1) |
|||
(tab Fmt "MDR" ": " "Values") |
|||
(tab Fmt "===" " " "======") |
|||
(for (I . S) *Solutions |
|||
(tab Fmt (dec I) ": " (glue ", " S)) ) )</lang> |
|||
{{out}} |
|||
<pre>Values MDR MP |
|||
====== === == |
|||
123321 8 3 |
|||
7739 8 3 |
|||
893 2 3 |
|||
899998 0 2 |
|||
MDR: Values |
|||
=== ====== |
|||
0: 0, 10, 20, 25, 30 |
|||
1: 1, 11, 111, 1111, 11111 |
|||
2: 2, 12, 21, 26, 34 |
|||
3: 3, 13, 31, 113, 131 |
|||
4: 4, 14, 22, 27, 39 |
|||
5: 5, 15, 35, 51, 53 |
|||
6: 6, 16, 23, 28, 32 |
|||
7: 7, 17, 71, 117, 171 |
|||
8: 8, 18, 24, 29, 36 |
|||
9: 9, 19, 33, 91, 119</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |