Jump to content

Four is magic: Difference between revisions

→‎{{header|UNIX Shell}}: Add implementation
(→‎{{header|UNIX Shell}}: Add implementation)
Line 2,169:
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|UNIX Shell}}==
{{works with|Bash|4+}}
<lang sh># Names for numbers that fit in a bash integer
declare -A 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 [12]=twelve
[13]=thirteen [14]=fourteen [15]=fifteen [16]=sixteen
[17]=seventeen [18]=eighteen [19]=nineteen [20]=twenty
[30]=thirty [40]=forty [50]=fifty [60]=sixty [70]=seventy
[80]=eighty [90]=ninety [100]=hundred [1000]=thousand
[1000000]=million [1000000000]=billion [1000000000000]=trillion
[1000000000000000]=quadrillion [1000000000000000000]=quintillion)
 
# The powers of 10 above 10, in descending order
powers_of_10=($(printf '%s\n' "${!names[@]}" | sort -nr | grep '00$'))
 
# Function to return the name of a number given in numeric form
# If the second parameter is passed in, it is appended to the name
# with a space between them; this is to facilitate tail recursion
 
name_of() {
local -i n=$1
local suffix=$2
 
# check for easy case first - exclude exact powers of 10
# as we'll need to stick a "one" in front of those.
if [[ $n != *00 && -n ${names[$n]} ]]; then
printf '%s%s\n' "${names[$n]}" ${suffix:+" $suffix"}
return 0
fi
 
# find the largest power of 10 that n is smaller than n
local -i i=0
local -i p=${powers_of_10[i]}
while (( p > n && i < ${#powers_of_10[@]}-1 )); do
i=i+1
p=${powers_of_10[$i]}
done
 
# if we found one, split on it and construct quotient 'name' remainder
if (( n >= p )); then
local -i quotient=n/p
local -i remainder=n%p
local remname=
if (( remainder > 0 )); then
remname=$(name_of $remainder)
fi
name_of "$quotient" "${names[$p]}${remname:+ $remname}${suffix:+ $suffix}"
else
# things are a little different under 100, since the multiples of
# 10 have their own names
local -i remainder=n%10
local -i tens=n-remainder
local remname=
if (( remainder > 0 )); then
remname=-$(name_of $remainder)
fi
printf '%s%s%s\n' "${names[$tens]}" "${remname}" ${suffix:+" $suffix"}
fi
 
return 0
}
 
# Convert numbers into the length of their names
# Display the series of values in name form until
# the length turns into four; then terminate with "four is magic."
# Again, takes a second argument, this time a prefix, to
# facilitate tail recursion.
four_is_magic() {
local -i n=$1
local prefix=$2
local name=$(name_of $n)
 
# capitalize the first entry
if [[ -z $prefix ]]; then
name=${name^}
fi
 
# Stop at 4, otherwise count the length of the name and recurse
if (( $n == 4 )); then
printf '%s%s is magic.\n' "${prefix:+$prefix, }" "$name"
else
local -i len=${#name}
four_is_magic "$len" "${prefix:+$prefix, }$name is $(name_of $len)"
fi
}</lang>
 
{{Out}}
<pre>
# sadly the number of Rubk's cube permutations won't fit into a bash int,
# but this is that value minus the first digit (40 quintillion)
$ four_is_magic 3252003274489856000
Three quintillion two hundred fifty-two quadrillion three trillion two hundred seventy-four billion four hundred eighty-nine million eight hundred fifty-six thousand is one hundred sixty-five, one hundred sixty-five is twenty-two, twenty-two is ten, ten is three, three is five, five is four, four is magic.
 
# Max Unicode code point
$ four_is_magic 1114111
One million one hundred fourteen thousand one hundred eleven is sixty, sixty is five, five is four, four is magic.
 
# Some usual suspects
$ four_is_magic 42
Forty-two is nine, nine is four, four is magic.
 
$ four_is_magic 23
Twenty-three is twelve, twelve is six, six is three, three is five, five is four, four is magic.
 
# the units
for (( i=0; i<10; i++ )); do
four_is_magic $i
done
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.
 
 
=={{header|Wren}}==
1,481

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.