Four is magic: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) (Added AutoHotkey) |
|||
Line 57: | Line 57: | ||
<br><br> |
<br><br> |
||
=={{header|AutoHotkey}}== |
|||
Based on [http://www.rosettacode.org/wiki/Number_names#AutoHotkey Number names] |
|||
<lang AutoHotkey>Four_is_magic(num){ |
|||
nubmer := num |
|||
while (num <> 4) |
|||
result .= (res := spell(num)) " is " spell(num := StrLen(res)) ", " |
|||
return PrettyNumber(nubmer) " " result "four is magic!" |
|||
} |
|||
Spell(n) { ; recursive function to spell out the name of a max 36 digit integer, after leading 0s removed |
|||
Static p1=" thousand ",p2=" million ",p3=" billion ",p4=" trillion ",p5=" quadrillion ",p6=" quintillion " |
|||
, p7=" sextillion ",p8=" septillion ",p9=" octillion ",p10=" nonillion ",p11=" decillion " |
|||
, t2="twenty",t3="thirty",t4="forty",t5="fifty",t6="sixty",t7="seventy",t8="eighty",t9="ninety" |
|||
, o0="zero",o1="one",o2="two",o3="three",o4="four",o5="five",o6="six",o7="seven",o8="eight" |
|||
, o9="nine",o10="ten",o11="eleven",o12="twelve",o13="thirteen",o14="fourteen",o15="fifteen" |
|||
, o16="sixteen",o17="seventeen",o18="eighteen",o19="nineteen" |
|||
If (11 < d := (StrLen(n)-1)//3) ; #of digit groups of 3 |
|||
Return "Number too big" |
|||
If (d) ; more than 3 digits |
|||
Return Spell(SubStr(n,1,-3*d)) p%d% ((s:=SubStr(n,1-3*d)) ? ", " Spell(s) : "") |
|||
i := SubStr(n,1,1) |
|||
If (n > 99) ; 3 digits |
|||
Return o%i% " hundred" ((s:=SubStr(n,2)) ? " " Spell(s) : "") |
|||
If (n > 19) ; n = 20..99 |
|||
Return t%i% ((o:=SubStr(n,2)) ? "-" o%o% : "") |
|||
Return o%n% ; n = 0..19 |
|||
} |
|||
PrettyNumber(n) { ; inserts thousands separators into a number string |
|||
Return RegExReplace(n, "\B(?=((\d{3})+$))", ",") |
|||
}</lang> |
|||
Examples:<lang AutoHotkey>for i, num in StrSplit("7,54,235,8463,95723,485723,5472539,15750268,853956201,2736452849,94837286837,636478294710", ",") |
|||
result .= Four_is_magic(num) "`n" |
|||
MsgBox % result</lang> |
|||
Outputs:<pre>7 seven is five, five is four, four is magic! |
|||
54 fifty-four is ten, ten is three, three is five, five is four, four is magic! |
|||
235 two hundred thirty-five is twenty-three, twenty-three is twelve, twelve is six, six is three, three is five, five is four, four is magic! |
|||
8,463 eight thousand , four hundred sixty-three is forty-one, forty-one is nine, nine is four, four is magic! |
|||
95,723 ninety-five thousand , seven hundred twenty-three is forty-nine, forty-nine is ten, ten is three, three is five, five is four, four is magic! |
|||
485,723 four hundred eighty-five thousand , seven hundred twenty-three is sixty-two, sixty-two is nine, nine is four, four is magic! |
|||
5,472,539 five million , four hundred seventy-two thousand , five hundred thirty-nine is seventy-five, seventy-five is twelve, twelve is six, six is three, three is five, five is four, four is magic! |
|||
15,750,268 fifteen million , seven hundred fifty thousand , two hundred sixty-eight is seventy-two, seventy-two is eleven, eleven is six, six is three, three is five, five is four, four is magic! |
|||
853,956,201 eight hundred fifty-three million , nine hundred fifty-six thousand , two hundred one is eighty-five, eighty-five is eleven, eleven is six, six is three, three is five, five is four, four is magic! |
|||
2,736,452,849 two billion , seven hundred thirty-six million , four hundred fifty-two thousand , eight hundred forty-nine is one hundred seven, one hundred seven is seventeen, seventeen is nine, nine is four, four is magic! |
|||
94,837,286,837 ninety-four billion , eight hundred thirty-seven million , two hundred eighty-six thousand , eight hundred thirty-seven is one hundred nineteen, one hundred nineteen is twenty, twenty is six, six is three, three is five, five is four, four is magic! |
|||
636,478,294,710 six hundred thirty-six billion , four hundred seventy-eight million , two hundred ninety-four thousand , seven hundred ten is one hundred twenty-two, one hundred twenty-two is twenty-two, twenty-two is ten, ten is three, three is five, five is four, four is magic!</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<lang AWK> |
<lang AWK> |