Function Repository Resource:

MagicInteger

Source Notebook

Iterate through integer names until an integer equal to its string length is reached

Contributed by: Kevin Reiss and Zach Shelton

ResourceFunction["MagicInteger"][n]

returns a string describing the integer n, then the StringLength of n, etc., until the "magic" integer 4 is reached.

Details

This game is based off the fact that 4 is the only integer equal to the number of characters in its name. This is often referred to as "4 is magic" or "4 is cosmic."
This function serves as a solution to the Rosetta Code problem Four Is Magic.
Negative inputs are allowed. For example, -9 would be “minus nine.”
The naming convention of integers follows "Cardinal" form from IntegerName.

Examples

Basic Examples (2) 

4 is magic:

In[1]:=
ResourceFunction["MagicInteger"][4]
Out[1]=

"One" has three letters, etc., up until the magic number 4:

In[2]:=
ResourceFunction["MagicInteger"][1]
Out[2]=

Applications (1) 

Find all of the magic numbers below 1000:

In[3]:=
Union[Capitalize /@ Table[StringSplit[ResourceFunction["MagicInteger"][n], ", "][[-1]], {n, 1000}]]
Out[3]=

Properties and Relations (1) 

Inputs less than 1 can't be magic since every number has more than 0 characters in its name:

In[4]:=
ResourceFunction["MagicInteger"][0]
Out[4]=
In[5]:=
ResourceFunction["MagicInteger"][-8]
Out[5]=

Neat Examples (1) 

Find the smallest input under 1000 that has the most iterates by counting the number of commas in the output:

In[6]:=
MaximalBy[Table[ResourceFunction["MagicInteger"][n], {n, 1000}], StringCount[#, ","] &][[1]]
Out[6]=

Publisher

Kevin Reiss

Version History

  • 1.0.0 – 28 June 2021

License Information