Jump to content

Four is magic: Difference between revisions

(→‎{{header|Ruby}}: Added Ruby)
(→‎{{header|Ruby}}: wrong place)
Line 1,965:
874143425855745733896030 :
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|Ruby}}==
Using a 'refinement' to the Integer class, a way to a way to extend a class locally.
 
<lang ruby>module NumberToWord
NUMBERS = { # taken from https://en.wikipedia.org/wiki/Names_of_large_numbers#cite_ref-a_14-3
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',
10 ** 6 => 'million',
10 ** 9 => 'billion',
10 ** 12 => 'trillion',
10 ** 15 => 'quadrillion',
10 ** 18 => 'quintillion',
10 ** 21 => 'sextillion',
10 ** 24 => 'septillion',
10 ** 27 => 'octillion',
10 ** 30 => 'nonillion',
10 ** 33 => 'decillion'}.reverse_each.to_h
refine Integer do
def to_english
return 'zero' if i.zero?
words = self < 0 ? ['negative'] : []
i = self.abs
NUMBERS.each do |k, v|
if k <= i then
times = i/k
words << times.to_english if k >= 100
words << v
i -= times * k
end
return words.join(" ") if i.zero?
end
end
end
end
using NumberToWord
def magic4(n)
words = []
until n == 4
s = n.to_english
n = s.size
words << "#{s} is #{n.to_english}"
end
words << "four is magic."
words.join(", ").capitalize
end
[0, 4, 6, 11, 13, 75, 337, -164, 9_876_543_209].each{|n| puts magic4(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.
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 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|Racket}}==
<lang racket>
1,149

edits

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