Four is magic: Difference between revisions

Added Easylang
No edit summary
(Added Easylang)
 
(13 intermediate revisions by 10 users not shown)
Line 57:
:*   [[De Bruijn sequences]]
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">V Small = [‘zero’, ‘one’, ‘two’, ‘three’, ‘four’,
‘five’, ‘six’, ‘seven’, ‘eight’, ‘nine’,
‘ten’, ‘eleven’, ‘twelve’, ‘thirteen’, ‘fourteen’,
‘fifteen’, ‘sixteen’, ‘seventeen’, ‘eighteen’, ‘nineteen’]
 
V Tens = [‘’, ‘’, ‘twenty’, ‘thirty’, ‘forty’, ‘fifty’, ‘sixty’, ‘seventy’, ‘eighty’, ‘ninety’]
 
V Illions = [‘’, ‘ thousand’, ‘ million’, ‘ billion’, ‘ trillion’, ‘ quadrillion’, ‘ quintillion’]
 
F say(Int64 =n) -> String
V result = ‘’
I n < 0
result = ‘negative ’
n = -n
 
I n < 20
result ‘’= Small[Int(n)]
 
E I n < 100
result ‘’= Tens[Int(n I/ 10)]
V m = n % 10
I m != 0
result ‘’= ‘-’Small[Int(m)]
 
E I n < 1000
result ‘’= Small[Int(n I/ 100)]‘ hundred’
V m = n % 100
I m != 0
result ‘’= ‘ ’say(m)
 
E
V sx = ‘’
V i = 0
L n > 0
V m = n % 1000
n I/= 1000
I m != 0
V ix = say(m)‘’Illions[i]
I sx.len > 0
ix ‘’= ‘ ’sx
sx = ix
i++
result ‘’= sx
 
R result
 
F fourIsMagic(=n)
V s = say(n).capitalize()
V result = s
L n != 4
n = s.len
s = say(n)
result ‘’= ‘ is ’s‘, ’s
R result‘ is magic.’
 
L(n) [Int64(0), 4, 6, 11, 13, 75, 100, 337, -164, 7FFF'FFFF'FFFF'FFFF]
print(fourIsMagic(n))</syntaxhighlight>
 
{{out}}
<pre>
Zero is four, four is magic.
Four is magic.
Six is three, three is five, five is four, four is magic.
Eleven is six, six is three, three is five, five is four, four is magic.
Thirteen is eight, eight is five, five is four, four is magic.
Seventy-five is twelve, twelve is six, six is three, three is five, five is four, four is magic.
One hundred is eleven, eleven is six, six is three, three is five, five is four, four is magic.
Three hundred thirty-seven is twenty-six, twenty-six is ten, ten is three, three is five, five is four, four is magic.
Negative one hundred sixty-four is thirty-one, thirty-one is ten, ten is three, three is five, five is four, four is magic.
Nine quintillion two hundred twenty-three quadrillion three hundred seventy-two trillion thirty-six billion eight hundred fifty-four million seven hundred seventy-five thousand eight hundred seven is one hundred ninety-six, one hundred ninety-six is twenty-two, twenty-two is ten, ten is three, three is five, five is four, four is magic.
</pre>
 
=={{header|8086 Assembly}}==
<langsyntaxhighlight lang="asm">puts: equ 9h ; MS-DOS syscall to print a string
cpu 8086
bits 16
Line 261 ⟶ 336:
errhigh: db 'Max input 999999$'
section .bss
numstring: resb 1024</langsyntaxhighlight>
 
{{out}}
Line 284 ⟶ 359:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">magic←{
t20←'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine'
t20←t20,'ten' 'eleven' 'twelve' 'thirteen' 'fourteen' 'fifteen' 'sixteen'
Line 310 ⟶ 385:
n,' is ',(spell ≢n),', ',∇≢n
}⍵
}</langsyntaxhighlight>
{{out}}
<pre> magic 0
Line 330 ⟶ 405:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">(* Uses a Foundation number formatter for brevity. *)
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
Line 382 ⟶ 457:
set end of output to fourIsMagic(n)
end repeat
return join(output, linefeed)</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"Negative nineteen is seventeen, seventeen is nine, nine is four, four is magic.
Zero is four, four is magic.
Four is magic.
Line 391 ⟶ 466:
Thirty-two is ten, ten is three, three is five, five is four, four is magic.
One hundred eleven is eighteen, eighteen is eight, eight is five, five is four, four is magic.
One billion two hundred thirty-four million five hundred sixty-five thousand seven hundred eighty-nine is one hundred two, one hundred two is fifteen, fifteen is seven, seven is five, five is four, four is magic."</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
Based on [http://www.rosettacode.org/wiki/Number_names#AutoHotkey Number names]
<langsyntaxhighlight AutoHotkeylang="autohotkey">Four_is_magic(num){
nubmer := num
while (num <> 4)
Line 421 ⟶ 496:
PrettyNumber(n) { ; inserts thousands separators into a number string
Return RegExReplace(n, "\B(?=((\d{3})+$))", ",")
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="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</langsyntaxhighlight>
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!
Line 439 ⟶ 514:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FOUR_IS_MAGIC.AWK
BEGIN {
Line 502 ⟶ 577:
split("ten twenty thirty forty fifty sixty seventy eighty ninety",tens," ")
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 526 ⟶ 601:
=={{header|C}}==
{{libheader|GLib}}
<langsyntaxhighlight lang="c">#include <stdint.h>
#include <stdio.h>
#include <glib.h>
Line 623 ⟶ 698:
test_magic(10344658531277200972ULL);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 641 ⟶ 716:
=={{header|C++}}==
Negative numbers are not supported.
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <string>
#include <cctype>
Line 746 ⟶ 821:
test_magic(10344658531277200972ULL);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 764 ⟶ 839:
=={{header|Clojure}}==
{{trans|UNIX Shell}}
<langsyntaxhighlight lang="clojure">(require '[clojure.edn :as edn])
(def names { 0 "zero" 1 "one" 2 "two" 3 "three" 4 "four" 5 "five"
6 "six" 7 "seven" 8 "eight" 9 "nine" 10 "ten" 11 "eleven"
Line 813 ⟶ 888:
(if (not= "repl" *command-line-args*)
(apply -main *command-line-args*))
</syntaxhighlight>
</lang>
 
{{Out}}
Line 834 ⟶ 909:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight Lisplang="lisp">(defun integer-to-text (int)
(format nil "~@(~A~)" (with-output-to-string (out)
(loop for n = int then (length c)
Line 840 ⟶ 915:
while (/= n 4)
do (format out "~A is ~R, " c (length c))
finally (format out "four is magic.")))))</langsyntaxhighlight>
 
{{out}}
Line 849 ⟶ 924:
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Four_is_magic;
 
Line 958 ⟶ 1,033:
writeln(fourIsMagic(n));
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>Zero is four, four is magic.
Line 970 ⟶ 1,045:
Negative one hundred and sixty-four is thirty-five, thirty-five is eleven, eleven is six, six is three, three is five, five is four, four is magic.
Nine quintillion, two hundred and twenty-three quadrillion, three hundred and seventy-two trillion, thirty-six billion, eight hundred and fifty-four million, seven hundred and seventy-five thousand, eight hundred and seven is two hundred and twenty-two, two hundred and twenty-two is twenty-six, twenty-six is ten, ten is three, three is five, five is four, four is magic.</pre>
 
=={{header|EasyLang}}==
{{trans|Go}}
<syntaxhighlight>
small$[] = [ "zero" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen" "eighteen" "nineteen" ]
tens$[] = [ "" "" "twenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety" ]
illions$[] = [ "" " thousand" " million" " billion" " trillion" " quadrillion" " quintillion" ]
func$ say n .
if n < 0
t$ = "negative "
n = -n
.
if n < 20
t$ &= small$[n + 1]
elif n < 100
t$ &= tens$[n div 10 + 1]
s = n mod 10
if s > 0
t$ &= "-" & small$[s + 1]
.
elif n < 1000
t$ &= small$[n div 100 + 1] & " hundred"
s = n mod 100
if s > 0
t$ &= " " & say s
.
else
i = 1
while n > 0
p = n mod 1000
n = n div 1000
if p > 0
ix$ = say p & illions$[i]
if sx$ <> ""
ix$ &= " " & sx$
.
sx$ = ix$
.
i += 1
.
t$ &= sx$
.
return t$
.
#
func$ toupper c$ .
c = strcode c$
if c >= 97 and c <= 122
c$ = strchar (c - 32)
.
return c$
.
func$ four_is_magic n .
s$ = say n
s$ = toupper substr s$ 1 1 & substr s$ 2 99999
t$ = s$
while n <> 4
n = len s$
s$ = say n
t$ &= " is " & s$ & ", " & s$
.
t$ &= " is magic."
return t$
.
for n in [ 6 13 75 111 337 99999999 ]
print four_is_magic n
.
</syntaxhighlight>
{{out}}
<pre>
Six is three, three is five, five is four, four is magic.
Thirteen is eight, eight is five, five is four, four is magic.
Seventy-five is twelve, twelve is six, six is three, three is five, five is four, four is magic.
One hundred eleven is eighteen, eighteen is eight, eight is five, five is four, four is magic.
Three hundred thirty-seven is twenty-six, twenty-six is ten, ten is three, three is five, five is four, four is magic.
Ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine is seventy-eight, seventy-eight is thirteen, thirteen is eight, eight is five, five is four, four is magic.
</pre>
 
=={{header|F_Sharp|F#}}==
===The Function===
<langsyntaxhighlight lang="fsharp">
//Express an Integer in English Language. Nigel Galloway: September 19th., 2018
let fN=[|[|"";"one";"two";"three";"four";"five";"six";"seven";"eight";"nine"|];
Line 982 ⟶ 1,134:
|α when α<1000 ->I2α (α-(α/100)*100) (β+fN.[0].[α/100]+" hunred"+if α%100>0 then " and " else "")
|α when α<1000000->I2α (α%1000) (β+(I2α (α/1000) "")+" thousand"+if α%100=0 then "" else if (α-(α/1000)*1000)<100 then " and " else " ")
</syntaxhighlight>
</lang>
===The Task===
<langsyntaxhighlight lang="fsharp">
let rec printI2α=function |0->printf "naught->"; printI2α 6
|4->printfn "four is magic"
Line 991 ⟶ 1,143:
let N=System.Random()
List.init 25 (fun _->N.Next 999999) |> List.iter printI2α
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,030 ⟶ 1,182:
=={{header|Factor}}==
Factor's <code>math.text.english</code> vocabulary does most of the heavy lifting. Since <code>number>text</code> produces <tt><i>" and "</i></tt> and <tt><i>","</i></tt> in its output, they are removed with a regular expression.
<langsyntaxhighlight lang="factor">USING: ascii formatting io kernel make math.text.english regexp
sequences ;
IN: rosetta-code.four-is-magic
Line 1,063 ⟶ 1,215:
len-chain [ phrase ] map concat capitalize print ;
{ 1 4 -11 100 112719908181724 -612312 } [ say-magic ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 1,075 ⟶ 1,227:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran">MODULE FOUR_IS_MAGIC
IMPLICIT NONE
CHARACTER(8), DIMENSION(20) :: SMALL_NUMS
Line 1,314 ⟶ 1,466:
ENDDO
END PROGRAM
</syntaxhighlight>
</lang>
<pre>
five is four, four is magic.
Line 1,330 ⟶ 1,482:
=={{header|FreeBASIC}}==
{{trans|Phix}}
<langsyntaxhighlight lang="freebasic">
#define floor(x) ((x*2.0-0.5) Shr 1)
 
Line 1,416 ⟶ 1,568:
Print Using "#######: &"; tests(i); fourIsMagic(tests(i))
Next i
Sleep</langsyntaxhighlight>
{{out}}
<pre>
Line 1,440 ⟶ 1,592:
123456: One centenas twenty-three thousand four centenas fifty-six is fifty-eight, fifty-eight is eleven, eleven is six, six is three, three is five, five is four, four is magic.
1010101: One million ten thousand one centenas one is forty-one, forty-one is nine, nine is four, four is magic.
</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn FourIsMagic( number as CFNumberRef ) as CFStringRef
CFMutableStringRef result = fn MutableStringNew
NumberFormatterRef formatter = fn NumberFormatterWithStyle( NSNumberFormatterSpellOutStyle )
NumberFormatterSetLocale( formatter, fn LocaleWithIdentifier( @"en_EN" ) )
CFStringRef numberString = fn NumberFormatterStringFromNumber( formatter, number )
MutableStringAppendString( result, fn StringCapitalizedString( numberString ) )
while ( fn StringIsEqual( numberString, @"four" ) == NO )
numberString = fn NumberFormatterStringFromNumber( formatter, fn NumberWithInteger( len(numberString) ) )
MutableStringAppendString( result, fn StringWithFormat( @" is %@, %@", numberString, numberString ) )
wend
MutableStringAppendString( result, @" is magic." )
end fn = result
 
NSInteger i
CFNumberRef testInput
CFArrayRef testNumbers : testNumbers = @[@23, @1000000000, @20140, @100, @130, @151, @-7]
 
NSLog( @"Outputs 0 through 9:\n" )
for i = 0 to 9
NSLog( @"%@", fn FourIsMagic( fn NumberWithInteger( i ) ) )
next
 
NSLog( @"\nOther number tests:\n" )
for testInput in testNumbers
NSLog( @"%@", fn FourIsMagic( testInput ) )
next
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Outputs 0 through 9:
 
Zero is four, four is magic.
One is three, three is five, five is four, four is magic.
Two is three, three is five, five is four, four is magic.
Three is five, five is four, four is magic.
Four is magic.
Five is four, four is magic.
Six is three, three is five, five is four, four is magic.
Seven is five, five is four, four is magic.
Eight is five, five is four, four is magic.
Nine is four, four is magic.
 
Other number tests:
 
Twenty-Three is twelve, twelve is six, six is three, three is five, five is four, four is magic.
One Billion is eleven, eleven is six, six is three, three is five, five is four, four is magic.
Twenty Thousand One Hundred Forty is thirty-three, thirty-three is twelve, twelve is six, six is three, three is five, five is four, four is magic.
One Hundred is eleven, eleven is six, six is three, three is five, five is four, four is magic.
One Hundred Thirty is eighteen, eighteen is eight, eight is five, five is four, four is magic.
One Hundred Fifty-One is twenty-one, twenty-one is ten, ten is three, three is five, five is four, four is magic.
Minus Seven is eleven, eleven is six, six is three, three is five, five is four, four is magic.
 
</pre>
 
Line 1,446 ⟶ 1,660:
Uses the <code>say</code> function from the
[[Number names#Go|Number names]] task.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,525 ⟶ 1,739:
}
return t
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,538 ⟶ 1,752:
Negative one hundred sixty-four is thirty-one, thirty-one is ten, ten is three, three is five, five is four, four is magic.
Nine quintillion two hundred twenty-three quadrillion three hundred seventy-two trillion thirty-six billion eight hundred fifty-four million seven hundred seventy-five thousand eight hundred seven is one hundred ninety-six, one hundred ninety-six is twenty-two, twenty-two is ten, ten is three, three is five, five is four, four is magic.
</pre>
 
=={{header|Haskell}}==
Negative numbers are supported.
 
<syntaxhighlight lang="haskell">module Main where
 
import Data.List (find)
import Data.Char (toUpper)
 
firstNums :: [String]
firstNums =
[ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
]
 
tens :: [String]
tens = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
 
biggerNumbers :: [(Int, String)]
biggerNumbers =
[(100, "hundred"), (1000, "thousand"), (1000000, "million"), (1000000000, "billion"), (1000000000000, "trillion")]
 
cardinal :: Int -> String
cardinal n
| n' < 20 =
negText ++ firstNums !! n'
| n' < 100 =
negText ++ tens !! (n' `div` 10 - 2) ++ if n' `mod` 10 /= 0 then "-" ++ firstNums !! (n' `mod` 10) else ""
| otherwise =
let (num, name) =
maybe
(last biggerNumbers)
fst
(find (\((num_, _), (num_', _)) -> n' < num_') (zip biggerNumbers (tail biggerNumbers)))
smallerNum = cardinal (n' `div` num)
in negText ++ smallerNum ++ " " ++ name ++ if n' `mod` num /= 0 then " " ++ cardinal (n' `mod` num) else ""
where
n' = abs n
negText = if n < 0 then "negative " else ""
 
capitalized :: String -> String
capitalized (x : xs) = toUpper x : xs
capitalized [] = []
 
magic :: Int -> String
magic =
go True
where
go first num =
let cardiNum = cardinal num
in (if first then capitalized else id) cardiNum ++ " is "
++ if num == 4
then "magic."
else cardinal (length cardiNum) ++ ", " ++ go False (length cardiNum)
 
main :: IO ()
main = do
putStrLn $ magic 3
putStrLn $ magic 15
putStrLn $ magic 4
putStrLn $ magic 10
putStrLn $ magic 20
putStrLn $ magic (-13)
putStrLn $ magic 999999
</syntaxhighlight>
{{out}}
<pre>
Three is five, five is four, four is magic.
Fifteen is seven, seven is five, five is four, four is magic.
Four is magic.
Ten is three, three is five, five is four, four is magic.
Twenty is six, six is three, three is five, five is four, four is magic.
Negative thirteen is seventeen, seventeen is nine, nine is four, four is magic.
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.
</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
names =. 'one';'two';'three';'four';'five';'six';'seven';'eight';'nine';'ten';'eleven';'twelve';'thirteen';'fourteen';'fifteen';'sixteen';'seventeen';'eighteen';'nineteen'
 
Line 1,586 ⟶ 1,876:
doall inputs
 
</syntaxhighlight>
</lang>
{{out}}
 
Line 1,612 ⟶ 1,902:
 
=={{header|Java}}==
<syntaxhighlight lang="java">
<lang Java>
public class FourIsMagic {
 
Line 1,686 ⟶ 1,976:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,717 ⟶ 2,007:
 
To test whether a particular JavaScript interpreter implements <code>BigInt</code>, we can evaluate a boolean expression like:
<langsyntaxhighlight lang="javascript">Object.getOwnPropertyNames(this).includes('BigInt')</langsyntaxhighlight>
 
<langsyntaxhighlight lang="javascript">const reverseOrderedNumberToTextMap = (function () {
const rawNumberToTextMapping = { // Ported over from the Python solution.
[1n]: "one",
Line 1,899 ⟶ 2,189:
-4,
10n ** 3003n + 42n
].map(fourIsMagic).join("\n\n");</langsyntaxhighlight>
 
{{output}}
Line 1,917 ⟶ 2,207:
One millinillion forty two is twenty six, twenty six is ten, ten is three, three is five, five is four, four is magic.
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
 
'''Also works with fq, a Go implementation of a large subset of jq'''
<syntaxhighlight lang=jq>
def small: ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
 
def tens: ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
 
def illions: ["", " thousand", " million", " billion"," trillion", " quadrillion", " quintillion"];
 
def say:
{n: ., t: ""}
| if .n < 0
then .t = "negative " | .n = -.n
else . end
| if .n < 20
then .t += small[.n]
elif .n < 100
then .t += tens[(.n/10)|floor]
| .s = .n % 10
| if (.s > 0) then .t += "-" + small[.s] else . end
elif .n < 1000
then .t += small[(.n/100)|floor] + " hundred"
| .s = .n % 100
| if .s > 0 then .t += " " + (.s|say) else . end
else .sx = ""
| .i = 0
| until(.n == 0;
.p = .n % 1000
| .n = (.n / 1000 |floor)
| if (.p > 0)
then .ix = (.p|say) + illions[.i]
| if (.sx != "") then .ix += " " + .sx else . end
| .sx = .ix
else .
end
| .i += 1 )
| .t += .sx
end
| .t;
 
def capitalize:
.[:1] as $x
| ($x | ascii_upcase) as $X
| if $x == $X then . else $X + .[1:] end;
def fourIsMagic:
{n: ., s: (say | capitalize)}
| .t = .s
| until(.n == 4;
.n = (.s|length)
| .s = (.n | say)
| .t += " is " + .s + ", " + .s )
| .t + " is magic." ;
 
(0, 4, 6, 11, 13, 75, 100, 337, -164, 9007199254740991)
| fourIsMagic
</syntaxhighlight>
'''Invocation:''' jq -rn -f four-is-magic.jq
{{output}}
As for [[#Wren|Wren]].
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia"># The num2text routines are from the "Number names" task, updated for Julia 1.0
 
const stext = ["one", "two", "three", "four", "five",
Line 2,020 ⟶ 2,376:
magic(n)
end
</langsyntaxhighlight> {{output}} <pre>
Zero is four, four is magic.
Four is magic.
Line 2,034 ⟶ 2,390:
=={{header|Kotlin}}==
This uses the code I wrote for the [[Number names]] task, appropriately adjusted to deal with this task. Input is limited to '''signed''' 64 bit integers as Kotlin doesn't currently support unsigned types.
<langsyntaxhighlight lang="scala">// version 1.1.4-3
 
val names = mapOf(
Line 2,141 ⟶ 2,497:
println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,167 ⟶ 2,523:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- Four is magic, in Lua, 6/16/2020 db
local oneslist = { [0]="", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }
local teenlist = { [0]="ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }
Line 2,210 ⟶ 2,566:
for _, num in ipairs(numbers) do
print(num, fourismagic(num))
end</langsyntaxhighlight>
{{out}}
<pre>-21 Negative twenty-one is nineteen, nineteen is eight, eight is five, five is four, four is magic.
Line 2,238 ⟶ 2,594:
Define a simple function which generates the output, using FixedPointList to iterate until a magic number is reached.
 
<langsyntaxhighlight Mathematicalang="mathematica">magic[num_] := Capitalize[ StringRiffle[ Partition[
FixedPointList[IntegerName[StringLength[#], "Cardinal"] &, IntegerName[num, "Cardinal"]],
2, 1] /. {n_, n_} :> {n, "magic"}, ", ", " is "] <> "."]</langsyntaxhighlight>
 
Call the function a few times to show the expected output:
Line 2,263 ⟶ 2,619:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import strutils
 
const
Line 2,329 ⟶ 2,685:
 
for n in [int64 0, 4, 6, 11, 13, 75, 100, 337, -164, int64.high]:
echo fourIsMagic(n)</langsyntaxhighlight>
 
{{out}}
Line 2,345 ⟶ 2,701:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use Lingua::EN::Numbers qw(num2en);
 
sub cardinal {
Line 2,369 ⟶ 2,725:
}
 
print magic($_) for 0, 4, 6, 11, 13, 75, 337, -164, 9_876_543_209;</langsyntaxhighlight>
{{out}}
<pre>Zero is four, four is magic.
Line 2,382 ⟶ 2,738:
 
=={{header|Phix}}==
Note that on 32-bit Phix integers/atoms are only accurate to 9,007,199,254,740,992 (a hardware limit of 64-bit floating point registers) and on 64-bit the limit is 18,446,744,073,709,551,616 (ditto 80-bit floating points) so if you need more than that this will need to be reworked to use bigatomsgmp or similar.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>--<adapted from demo\rosetta\number_names.exw, which alas outputs ",", "and", uses "minus" instead of "negative", etc...>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
constant twenties = {"zero","one","two","three","four","five","six","seven","eight","nine","ten",
<span style="color: #000080;font-style:italic;">--&lt;adapted from demo\rosetta\number_names.exw, which alas outputs ",", "and", uses "minus" instead of "negative", etc...&gt;</span>
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"},
<span style="color: #008080;">constant</span> <span style="color: #000000;">twenties</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"zero"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"one"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"two"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"three"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"four"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"five"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"six"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"seven"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"eight"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"nine"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ten"</span><span style="color: #0000FF;">,</span>
decades = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}
<span style="color: #008000;">"eleven"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"twelve"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"thirteen"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"fourteen"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"fifteen"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"sixteen"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"seventeen"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"eighteen"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"nineteen"</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">decades</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"twenty"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"thirty"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"forty"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"fifty"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"sixty"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"seventy"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"eighty"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ninety"</span><span style="color: #0000FF;">}</span>
function hundred(integer n)
if n<20 then
<span style="color: #008080;">function</span> <span style="color: #000000;">hundred</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
return twenties[mod(n,20)+1]
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">20</span> <span style="color: #008080;">then</span>
elsif mod(n,10)=0 then
<span style="color: #008080;">return</span> <span style="color: #000000;">twenties</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
return decades[mod(floor(n/10),10)-1]
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
end if
<span style="color: #008080;">return</span> <span style="color: #000000;">decades</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
return decades[mod(floor(n/10),10)-1] & '-' & twenties[mod(n,10)+1]
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">decades</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">&</span> <span style="color: #008000;">'-'</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">twenties</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function thousand(integer n)
if n<100 then
<span style="color: #008080;">function</span> <span style="color: #000000;">thousand</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
return hundred(n)
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">100</span> <span style="color: #008080;">then</span>
elsif mod(n,100)=0 then
<span style="color: #008080;">return</span> <span style="color: #000000;">hundred</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
return twenties[mod(floor(n/100),20)+1]&" hundred"
<span style="color: #008080;">elsif</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
end if
<span style="color: #008080;">return</span> <span style="color: #000000;">twenties</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]&</span><span style="color: #008000;">" hundred"</span>
return twenties[mod(floor(n/100),20)+1] & " hundred " & hundred(mod(n,100))
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">twenties</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">&</span> <span style="color: #008000;">" hundred "</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">hundred</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant orders = {{power(10,12),"trillion"},
{power(10,9),"billion"},
<span style="color: #008080;">constant</span> <span style="color: #000000;">orders</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"trillion"</span><span style="color: #0000FF;">},</span>
{power(10,6),"million"},
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"billion"</span><span style="color: #0000FF;">},</span>
{power(10,3),"thousand"}}
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"million"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"thousand"</span><span style="color: #0000FF;">}}</span>
function triplet(integer n)
atom order, high, low
<span style="color: #008080;">function</span> <span style="color: #000000;">triplet</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
string name, res = ""
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
for i=1 to length(orders) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">orders</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
{order,name} = orders[i]
<span style="color: #0000FF;">{</span><span style="color: #004080;">atom</span> <span style="color: #000000;">order</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">orders</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
high = floor(n/order)
<span style="color: #004080;">atom</span> <span style="color: #000000;">high</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #000000;">order</span><span style="color: #0000FF;">),</span>
low = mod(n,order)
<span style="color: #000000;">low</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">order</span><span style="color: #0000FF;">)</span>
if high!=0 then
<span style="color: #008080;">if</span> <span style="color: #000000;">high</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
res &= thousand(high)&' '&name
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">thousand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">high</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">&</span><span style="color: #000000;">name</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
n = low
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">low</span>
if low=0 then exit end if
<span style="color: #008080;">if</span> <span style="color: #000000;">low</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if length(res) and high!=0 then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">high</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
res &= " "
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" "</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if n!=0 or res="" then
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span> <span style="color: #008080;">then</span>
res &= thousand(floor(n))
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">thousand</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return res
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function spell(integer n)
<span style="color: #008080;">function</span> <span style="color: #000000;">spell</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
string res = ""
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
if n<0 then
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
res = "negative "
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"negative "</span>
n = -n
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">n</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
res &= triplet(n)
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">triplet</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
return res
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
--</adapted from number_names.exw>
<span style="color: #000080;font-style:italic;">--&lt;/adapted from number_names.exw&gt;</span>
 
function fourIsMagic(atom n)
<span style="color: #008080;">function</span> <span style="color: #000000;">fourIsMagic</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
string s = spell(n)
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">spell</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
s[1] = upper(s[1])
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
string t = s
<span style="color: #004080;">string</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span>
while n!=4 do
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">4</span> <span style="color: #008080;">do</span>
n = length(s)
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
s = spell(n)
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">spell</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
t &= " is " & s & ", " & s
<span style="color: #000000;">t</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" is "</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">&</span> <span style="color: #008000;">", "</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">s</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
t &= " is magic.\n"
<span style="color: #000000;">t</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" is magic.\n"</span>
return t
<span style="color: #008080;">return</span> <span style="color: #000000;">t</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
constant tests = {-7, -1, 0, 1, 2, 3, 4, 23, 1e9, 20140, 100, 130, 151, 999999}
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">23</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1e9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">20140</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">100</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">130</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">151</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">999999</span><span style="color: #0000FF;">}</span>
for i=1 to length(tests) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
puts(1,fourIsMagic(tests[i]))
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fourIsMagic</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]))</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,482 ⟶ 2,840:
Python 3 version. Should work for integers up to at least 10^3003. It can be extended easily to arbitrary integers by adding to the numbers dict.
 
<langsyntaxhighlight lang="python">import random
from collections import OrderedDict
 
Line 2,645 ⟶ 3,003:
 
for j in (random.randint(-10 ** 24, 10 ** 24) for i in range(2)):
print(j, ':\n', magic(j), '\n')</langsyntaxhighlight>
 
{{out}}
Line 2,669 ⟶ 3,027:
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}}==
<langsyntaxhighlight lang="q">C:``one`two`three`four`five`six`seven`eight`nine`ten,
`eleven`twelve`thirteen`fourteen`fifteen`sixteen`seventeen`eighteen`nineteen / cardinal numbers <20
 
Line 2,684 ⟶ 3,042:
fim:{@[;0;upper],[;"four is magic.\n"] raze 1_{y," is ",x,", "}prior s each(count s@)\[x]} / four is magic
 
1 raze fim each 0 4 8 16 25 89 365 2586 25865 369854 40000000001; / tests</langsyntaxhighlight>
{{out}}
<pre>Zero is four, four is magic.
Line 2,700 ⟶ 3,058:
 
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.
<langsyntaxhighlight 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</langsyntaxhighlight>
* [https://code.kx.com/q/ref/ Language Reference]
* [https://code.kx.com/q/learn/pb/four-magic/ The Q Playbook: Four is magic – analysis]
 
=={{header|R}}==
<syntaxhighlight lang="r">
<lang R>
# provided by neonira
 
Line 2,774 ⟶ 3,132:
}
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,790 ⟶ 3,148:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 2,846 ⟶ 3,204:
(displayln (number-magic n))
(newline))
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,988 ⟶ 3,346:
Lingua::EN::Numbers module available from the [https://modules.raku.org/search/?q=Lingua%3A%3AEN%3A%3ANumbers Raku ecosystem].
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers; # Version 2.4.0 or higher
 
sub card ($n) { cardinal($n).subst(/','/, '', :g) }
Line 3,006 ⟶ 3,364:
}
 
.&magic.say for 0, 4, 6, 11, 13, 75, 337, -164, 9876543209, 2**256;</langsyntaxhighlight>
{{out}}
<pre style="width:98%;overflow:wrap;">Zero is four, four is magic.
Line 3,032 ⟶ 3,390:
 
Numbers are limited to 3,003 decimal digits, the maximum number that the &nbsp; '''$SPELL#''' &nbsp; REXX program will handle.
<langsyntaxhighlight lang="rexx">/*REXX pgm converts a # to English into the phrase: a is b, b is c, ... four is magic. */
numeric digits 3003 /*be able to handle gihugic numbers. */
parse arg x /*obtain optional numbers from the C.L.*/
Line 3,053 ⟶ 3,411:
$= strip($ 'four is magic.') /*finish the sentence with the finale. */
parse var $ first 2 other; upper first /*capitalize the first letter of output*/
return first || other /*return the sentence to the invoker. */</langsyntaxhighlight>
The &nbsp; '''$SPELL#.REX''' &nbsp; routine can be found here &nbsp; ───► &nbsp; [[$SPELL.REX|$SPELL#.REX]]. <br><br>
 
Line 3,081 ⟶ 3,439:
=={{header|Ring}}==
 
<langsyntaxhighlight lang="ring">
/* Checking numbers from 0 to 10 */
for c = 0 to 10
Line 3,192 ⟶ 3,550:
Return trim(Result)
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,210 ⟶ 3,568:
Using a 'refinement' to the Integer class, a way to a way to extend a class locally.
 
<langsyntaxhighlight lang="ruby">module NumberToWord
NUMBERS = { # taken from https://en.wikipedia.org/wiki/Names_of_large_numbers#cite_ref-a_14-3
Line 3,286 ⟶ 3,644:
[0, 4, 6, 11, 13, 75, 337, -164, 9_876_543_209].each{|n| puts magic4(n) }
</langsyntaxhighlight>
{{out}}<pre>Zero is four, four is magic.
Four is magic.
Line 3,299 ⟶ 3,657:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn main() {
magic(4);
magic(2_340);
Line 3,376 ⟶ 3,734:
}
return cur;
}</langsyntaxhighlight>
 
{{out}}
Line 3,397 ⟶ 3,755:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func cardinal(n) {
static lingua_en = frequire("Lingua::EN::Numbers")
lingua_en.num2en(n) - / and|,/g
Line 3,419 ⟶ 3,777:
[0, 4, 6, 11, 13, 75, 337, -164, 9_876_543_209].each { |n|
say four_is_magic(n)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,434 ⟶ 3,792:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func fourIsMagic(_ number: NSNumber) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .spellOut
Line 3,453 ⟶ 3,811:
result.append(" is magic.")
return result.joined()
}
}</lang>
 
for testInput in [23, 1000000000, 20140, 100, 130, 151, -7] {
print(fourIsMagic(testInput as NSNumber))
} </syntaxhighlight>
{{out}}
<pre>
Line 3,467 ⟶ 3,829:
=={{header|UNIX Shell}}==
{{works with|Bash|4+}}
<langsyntaxhighlight lang="sh">#!/usr/bin/env bash
name_of() {
# return the English name for a numeric value
Line 3,547 ⟶ 3,909:
four_is_magic "$len" "${prefix:+$prefix, }$name is $(name_of $len)"
fi
}</langsyntaxhighlight>
 
{{Out}}
Line 3,581 ⟶ 3,943:
Eight is five, five is four, four is magic.
Nine is four, four is magic.
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">import math
fn main() {
for n in [i64(0), 4, 6, 11, 13, 75, 100, 337, -164,
math.max_i64,
] {
println(four_is_magic(n))
}
}
fn four_is_magic(nn i64) string {
mut n := nn
mut s := say(n)
s = s[..1].to_upper() + s[1..]
mut t := s
for n != 4 {
n = i64(s.len)
s = say(n)
t += " is " + s + ", " + s
}
t += " is magic."
return t
}
const small = ["zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
const tens = ["", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"]
const illions = ["", " thousand", " million", " billion",
" trillion", " quadrillion", " quintillion"]
fn say(nn i64) string {
mut t := ''
mut n := nn
if n < 0 {
t = "negative "
// Note, for math.MinInt64 this leaves n negative.
n = -n
}
match true {
n < 20 {
t += small[n]
}
n < 100 {
t += tens[n/10]
s := n % 10
if s > 0 {
t += "-" + small[s]
}
}
n < 1000 {
t += small[n/100] + " hundred"
s := n % 100
if s > 0 {
t += " " + say(s)
}
}
else {
// work right-to-left
mut sx := ""
for i := 0; n > 0; i++ {
p := n % 1000
n /= 1000
if p > 0 {
mut ix := say(p) + illions[i]
if sx != "" {
ix += " " + sx
}
sx = ix
}
}
t += sx
}
}
return t
}</syntaxhighlight>
 
{{out}}
<pre>
Zero is four, four is magic.
Four is magic.
Six is three, three is five, five is four, four is magic.
Eleven is six, six is three, three is five, five is four, four is magic.
Thirteen is eight, eight is five, five is four, four is magic.
Seventy-five is twelve, twelve is six, six is three, three is five, five is four, four is magic.
One hundred is eleven, eleven is six, six is three, three is five, five is four, four is magic.
Three hundred thirty-seven is twenty-six, twenty-six is ten, ten is three, three is five, five is four, four is magic.
Negative one hundred sixty-four is thirty-one, thirty-one is ten, ten is three, three is five, five is four, four is magic.
Nine quadrillion seven trillion one hundred ninety-nine billion two hundred fifty-four million seven hundred forty thousand nine hundred ninety-one is one hundred forty-seven, one hundred forty-seven is twenty-three, twenty-three is twelve, twelve is six, six is three, three is five, five is four, four is magic.
</pre>
 
Line 3,589 ⟶ 4,044:
 
Note that it is not safe to use this script for numbers with an absolute magnitude >= 2^53 as integers cannot be expressed exactly by Wren's Num type beyond that limit.
<langsyntaxhighlight ecmascriptlang="wren">import "./str" for Str
 
var small = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
Line 3,614 ⟶ 4,069:
t = t + small[(n/100).floor] + " hundred"
var s = n % 100
System.write("") // guards against VM recursion bug
if (s > 0) t = t + " " + say.call(s)
} else {
Line 3,623 ⟶ 4,077:
n = (n/1000).floor
if (p > 0) {
System.write("") // guards against VM recursion bug
var ix = say.call(p) + illions[i]
if (sx != "") ix = ix + " " + sx
Line 3,648 ⟶ 4,101:
for (n in [0, 4, 6, 11, 13, 75, 100, 337, -164, 9007199254740991]) {
System.print(fourIsMagic.call(n))
}</langsyntaxhighlight>
 
{{out}}
Line 3,670 ⟶ 4,123:
Uses the nth function from [[Spelling_of_ordinal_numbers#zkl]]
 
<langsyntaxhighlight lang="zkl">fcn fourIsMagic(int){
if(int==0) return("Zero is four, four is magic.");
string:="";
Line 3,683 ⟶ 4,136:
}
string[0].toUpper() + string[1,*]
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach n in (T(0,4,6,11,13,75,337,-164,9876543209)){
println(fourIsMagic(n),"\n")
}</langsyntaxhighlight>
{{out}}
<pre>
2,033

edits