Four is magic: Difference between revisions

Added Easylang
m (syntax highlighting fixup automation)
(Added Easylang)
 
(5 intermediate revisions by 4 users not shown)
Line 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#}}==
Line 1,515 ⟶ 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 2,068 ⟶ 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}}==
Line 3,740 ⟶ 3,945:
</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,
Line 3,839 ⟶ 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.
<syntaxhighlight lang="ecmascriptwren">import "./str" for Str
 
var small = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
Line 3,864 ⟶ 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,873 ⟶ 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
1,983

edits