Four is magic: Difference between revisions

added Mathematica solution
(→‎{{header|UNIX Shell}}: Add support for negative numbers)
(added Mathematica solution)
Line 2,098:
1010101 One million ten thousand one hundred one is forty, forty is five, five is four, four is magic.</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
 
Define a simple function which generates the output, using FixedPointList to iterate until a magic number is reached.
 
<lang Mathematica>
magic[num_] := Capitalize[ StringRiffle[ Partition[
FixedPointList[IntegerName[StringLength[#], "Cardinal"] &, IntegerName[num, "Cardinal"]],
2, 1] /. {n_, n_} :> {n, "magic"}, ", ", " is "] <> "."]
</lang>
 
Call the function a few times to show the expected output:
{{out}}
<pre>
In[1]:= magic[0]
Out[1]= "Zero is four, four is magic."
 
In[2]:= magic[1]
Out[2]= "One is three, three is five, five is four, four is magic."
 
In[3]:= magic[10]
Out[3]= "Ten is three, three is five, five is four, four is magic."
 
In[4]:= magic[999999]
Out[4]= "Nine hundred ninety-nine thousand nine hundred ninety-nine is fifty-eight, fifty-eight is eleven, eleven is six, six is three, three is five, five is four, four is magic."
 
In[5]:= magic[RandomInteger[10^6]]
Out[5]= "One hundred ninety-four thousand sixty-four is forty-three, forty-three is eleven, eleven is six, six is three, three is five, five is four, four is magic."
</pre>
=={{header|Nim}}==
{{trans|Go}}
Anonymous user