Four is magic: Difference between revisions
Content added Content deleted
m (→{{header|q}}) |
m (→{{header|q}}: link to discussion article) |
||
Line 1,995: | Line 1,995: | ||
Forty billion one is seventeen, seventeen is nine, nine is four, four is magic.</pre> |
Forty billion one is seventeen, seventeen is nine, nine is four, four is magic.</pre> |
||
'''Comment''' |
'''Comment''' |
||
The solution is well modularised: <code>st</code> stringifies a number <1000; <code>s</code> stringifies a number; <code>fim</code> generates the sequence and formats it. |
|||
We see |
|||
* the Converge iterator <code>\</code> |
|||
* recursion through self-reference <code>.z.s</code> |
|||
* composition <code>count s@</code> |
|||
* Each Prior iterator <code>prior</code> |
|||
In q the same syntax applies a function to an argument or a list to its indexes. A consequence is that, with the Converge iterator <code>\</code> the lengths alone form a finite-state machine which can generate the convergence. |
In q the same syntax applies a function to an argument or a list to its indexes. A consequence is that, with the Converge iterator <code>\</code> the lengths alone form a finite-state machine which can generate the convergence. |
||
Line 2,010: | Line 2,003: | ||
q)sl\[19] |
q)sl\[19] |
||
19 8 5 4</lang> |
19 8 5 4</lang> |
||
⚫ | |||
Finally, we demonstrate the convergence using a cache of a million stringified numbers, and their lengths as a finite-state machine. |
|||
* [https://code.kx.com/q/learn/pb/four-magic/ The Q Playbook: Four is magic – analysis] |
|||
<lang q>q)sl:count each ten6:s each til 1000000 / stringify first million numbers and lengths |
|||
q)ten6 789123 |
|||
"seven hundred eighty nine thousand one hundred twenty three" |
|||
q)sl 789123 |
|||
59 |
|||
q)sl\[789123] / converge |
|||
789123 59 10 3 5 4 |
|||
q)1 fim 789123; / compare result |
|||
Seven hundred eighty nine thousand one hundred twenty three is fifty nine, fifty nine is ten, ten is three, three is five, five is four, four is magic.</lang> |
|||
⚫ | |||
=={{header|Racket}}== |
=={{header|Racket}}== |