Four is magic: Difference between revisions

Added Wren
m (C++ - changed code to comply with task requirements)
(Added Wren)
Line 2,165:
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 billion eight hundred seventy-six million five hundred forty-three thousand two hundred nine is ninety-seven, ninety-seven is twelve, twelve is six, six is three, three is five, five is four, four is magic.
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-str}}
This resuses the <code>say</code> function from the [[Number names#Wren|Number names]] task.
 
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.
<lang ecmascript>import "/str" for Str
 
var small = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
 
var tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
 
var illions = ["", " thousand", " million", " billion"," trillion", " quadrillion", " quintillion"]
 
var say
say = Fn.new { |n|
var t = ""
if (n < 0) {
t = "negative "
n = -n
}
if (n < 20) {
t = t + small[n]
} else if (n < 100) {
t = t + tens[(n/10).floor]
var s = n % 10
if (s > 0) t = t + "-" + small[s]
} else if (n < 1000) {
t = t + small[(n/100).floor] + " hundred"
var s = n % 100
if (s > 0) t = t + " " + say.call(s)
} else {
var sx = ""
var i = 0
while (n > 0) {
var p = n % 1000
n = (n/1000).floor
if (p > 0) {
var ix = say.call(p) + illions[i]
if (sx != "") ix = ix + " " + sx
sx = ix
}
i = i + 1
}
t = t + sx
}
return t
}
 
var fourIsMagic = Fn.new { |n|
var s = Str.capitalize(say.call(n))
var t = s
while (n != 4) {
n = s.count
s = say.call(n)
t = t + " is " + s + ", " + s
}
return t + " is magic."
}
 
for (n in [0, 4, 6, 11, 13, 75, 100, 337, -164, 9007199254740991]) {
System.print(fourIsMagic.call(n))
}</lang>
 
{{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>
 
9,476

edits