77 lines
2.1 KiB
Text
77 lines
2.1 KiB
Text
with javascript_semantics
|
|
constant twenties = {"zero","one","two","three","four","five","six","seven","eight","nine","ten",
|
|
"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"},
|
|
decades = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}
|
|
|
|
function hundred(integer n)
|
|
if n<20 then
|
|
return twenties[mod(n,20)+1]
|
|
elsif mod(n,10)=0 then
|
|
return decades[mod(floor(n/10),10)-1]
|
|
end if
|
|
return decades[mod(floor(n/10),10)-1] & '-' & twenties[mod(n,10)+1]
|
|
end function
|
|
|
|
function thousand(integer n)
|
|
if n<100 then
|
|
return hundred(n)
|
|
elsif mod(n,100)=0 then
|
|
return twenties[mod(floor(n/100),20)+1]&" hundred"
|
|
end if
|
|
return twenties[mod(floor(n/100),20)+1] & " hundred " & hundred(mod(n,100))
|
|
end function
|
|
|
|
constant orders = {{power(10,12),"trillion"},
|
|
{power(10,9),"billion"},
|
|
{power(10,6),"million"},
|
|
{power(10,3),"thousand"}}
|
|
|
|
function triplet(integer n)
|
|
string res = ""
|
|
for i=1 to length(orders) do
|
|
{atom order, string name} = orders[i]
|
|
atom high = floor(n/order),
|
|
low = mod(n,order)
|
|
if high!=0 then
|
|
res &= thousand(high)&' '&name
|
|
end if
|
|
n = low
|
|
if low=0 then exit end if
|
|
if length(res) and high!=0 then
|
|
res &= " "
|
|
end if
|
|
end for
|
|
if n!=0 or res="" then
|
|
res &= thousand(floor(n))
|
|
end if
|
|
return res
|
|
end function
|
|
|
|
function spell(integer n)
|
|
string res = ""
|
|
if n<0 then
|
|
res = "negative "
|
|
n = -n
|
|
end if
|
|
res &= triplet(n)
|
|
return res
|
|
end function
|
|
--</adapted from number_names.exw>
|
|
|
|
function fourIsMagic(atom n)
|
|
string s = spell(n)
|
|
s[1] = upper(s[1])
|
|
string t = s
|
|
while n!=4 do
|
|
n = length(s)
|
|
s = spell(n)
|
|
t &= " is " & s & ", " & s
|
|
end while
|
|
t &= " is magic.\n"
|
|
return t
|
|
end function
|
|
|
|
constant tests = {-7, -1, 0, 1, 2, 3, 4, 23, 1e9, 20140, 100, 130, 151, 999999}
|
|
for i=1 to length(tests) do
|
|
puts(1,fourIsMagic(tests[i]))
|
|
end for
|