Four is magic: Difference between revisions

→‎{{header|q}}: q solution
(→‎{{header|Ruby}}: Added Ruby)
(→‎{{header|q}}: q solution)
Line 1,965:
874143425855745733896030 :
Eight hundred seventy four sextillion one hundred forty three quintillion four hundred twenty five quadrillion eight hundred fifty five trillion seven hundred forty five billion seven hundred thirty three million eight hundred ninety six thousand thirty is two hundred fifty three, two hundred fifty three is twenty three, twenty three is twelve, twelve is six, six is three, three is five, five is four, four is magic. </pre>
=={{header|q}}==
<lang q>/ small cardinal numbers
C:``one`two`three`four`five`six`seven`eight`nine`ten`eleven`twelve`thirteen`fourteen`fifteen`sixteen`seventeen`eighteen`nineteen</lang>
A distinctive feature of q is that 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 form a finite-state machine which can generate the convergence without further logic.
<lang q>q)show sl:count each string C / string lengths
0 3 3 5 4 4 3 5 5 4 3 6 6 8 8 7 7 9 8 8
q)sl\[18]
18 8 5 4
q)sl\[19]
19 8 5 4</lang>
Stringify a number.
<lang q>/ tens
T:``ten`twenty`thirty`forty`fifty`sixty`seventy`eighty`ninety
/ magnitudes
M:``thousand`million`billion`trillion`quadrillion`quintillion`sextillion`septillion
 
st:{ / stringify <1000
$[x<20; C x;
x<100; (T;C)@'10 vs x;
{C[y],`hundred,$[z=0;`;x z]}[.z.s] . 100 vs x] }
 
/ stringify
s:{$[x=0; "zero"; {" "sv string except[;`]raze x{$[x~`;x;x,y]}'M reverse til count x} st each 1000 vs x]}
 
/ four is magic
fim:{@[;0;upper],[;"four is magic.\n"] raze 1_{y," is ",x,", "}prior s each(count s@)\[x]}
 
/ tests
1 raze fim each 0 4 8 16 25 89 365 2586 25865 369854 40000000001;</lang>
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 use of
* the Converge iterator <code>/</code>
* recursion through self-reference <code>.z.s</code>
* composition <code>count s@</code>
* Each Prior iterator <code>prior</code>
{{out}}
<pre>Zero is four, four is magic.
Four is magic.
Eight is five, five is four, four is magic.
Sixteen is seven, seven is five, five is four, four is magic.
Twenty five is eleven, eleven is six, six is three, three is five, five is four, four is magic.
Eighty nine is eleven, eleven is six, six is three, three is five, five is four, four is magic.
Three hundred sixty five is twenty four, twenty four is eleven, eleven is six, six is three, three is five, five is four, four is magic.
Two thousand five hundred eighty six is thirty six, thirty six is ten, ten is three, three is five, five is four, four is magic.
Twenty five thousand eight hundred sixty five is forty five, forty five is ten, ten is three, three is five, five is four, four is magic.
Three hundred sixty nine thousand eight hundred fifty four is fifty eight, fifty eight is eleven, eleven is six, six is three, three is five, five is four, four is magic.
Forty billion one is seventeen, seventeen is nine, nine is four, four is magic.</pre>
Finally, we demonstrate the convergence using a cache of a million stringified numbers, and their lengths as a finite-state machine.
<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>
[https://code.kx.com/q/ref/ Language Reference]
 
=={{header|Racket}}==
<lang racket>
38

edits