Four is magic: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: change link)
(Revamped to remove error)
Line 1,047: Line 1,047:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
{{incorrect|Racket|At least, the output is suspect; "Twenty thousand one hundred forty is three"?}}
#lang racket


(require rackunit)
<lang racket>#lang racket

(define number-names
(list "zero" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve"
"thirteen" "fourteen" "fifteen" "sixteen" "seventeen" "eighteen" "nineteen"))

(define numbers-alist
'((20 . twenty)
(30 . thirty)
(40 . forty)
(50 . fifty)
(60 . sixty)
(70 . seventy)
(80 . eighty)
(90 . ninety)
(100 . hundred)
(1000 . thousand)
(#e1E6 . million)
(#e1E9 . billion)
(#e1E12 . trillion)
(#e1E15 . quadrillion)
(#e1E18 . quintillion)
(#e1E21 . sextillion)
(#e1E24 . septillion)
(#e1E27 . octillion)
(#e1E30 . nonillion)
(#e1E33 . decillion)
(#e1E36 . undecillion)
(#e1E39 . duodecillion)
(#e1E42 . tredecillion)
(#e1E45 . quattuordecillion)
(#e1E48 . quinquadecillion)
(#e1E51 . sedecillion)
(#e1E54 . septendecillion)
(#e1E57 . octodecillion)
(#e1E60 . novendecillion)
(#e1E63 . vigintillion)
(#e1E66 . unvigintillion)
(#e1E69 . duovigintillion)
(#e1E72 . tresvigintillion)
(#e1E75 . quattuorvigintillion)
(#e1E78 . quinquavigintillion)
(#e1E81 . sesvigintillion)
(#e1E84 . septemvigintillion)
(#e1E87 . octovigintillion)
(#e1E90 . novemvigintillion)
(#e1E93 . trigintillion)
(#e1E96 . untrigintillion)
(#e1E99 . duotrigintillion)
(#e1E102 . trestrigintillion)
(#e1E105 . quattuortrigintillion)
(#e1E108 . quinquatrigintillion)
(#e1E111 . sestrigintillion)
(#e1E114 . septentrigintillion)
(#e1E117 . octotrigintillion)
(#e1E120 . noventrigintillion)
(#e1E123 . quadragintillion)
(#e1E153 . quinquagintillion)
(#e1E183 . sexagintillion)
(#e1E213 . septuagintillion)
(#e1E243 . octogintillion)
(#e1E273 . nonagintillion)
(#e1E303 . centillion)
(#e1E306 . uncentillion)
(#e1E309 . duocentillion)
(#e1E312 . trescentillion)
(#e1E333 . decicentillion)
(#e1E336 . undecicentillion)
(#e1E363 . viginticentillion)
(#e1E366 . unviginticentillion)
(#e1E393 . trigintacentillion)
(#e1E423 . quadragintacentillion)
(#e1E453 . quinquagintacentillion)
(#e1E483 . sexagintacentillion)
(#e1E513 . septuagintacentillion)
(#e1E543 . octogintacentillion)
(#e1E573 . nonagintacentillion)
(#e1E603 . ducentillion)
(#e1E903 . trecentillion)
(#e1E1203 . quadringentillion)
(#e1E1503 . quingentillion)
(#e1E1803 . sescentillion)
(#e1E2103 . septingentillion)
(#e1E2403 . octingentillion)
(#e1E2703 . nongentillion)
(#e1E3003 . millinillion)))


(define smalls
(map symbol->string
'(zero one two three four five six seven eight nine ten eleven twelve
thirteen fourteen fifteen sixteen seventeen eighteen nineteen)))
(define tens
(map symbol->string
'(zero ten twenty thirty forty fifty sixty seventy eighty ninety)))
(define larges
(map symbol->string
'(thousand million billion trillion quadrillion quintillion sextillion
septillion octillion nonillion decillion undecillion duodecillion
tredecillion quattuordecillion quindecillion sexdecillion
septendecillion octodecillion novemdecillion vigintillion)))
(define (number->words n)
(define (number->words n)
(define (n->list n acc)
(define (step div suffix separator [subformat number->words])
(define (name-of n) (symbol->string(cdr (assoc n numbers-alist))))
(define-values [q r] (quotient/remainder n div))
(define (cons-name n) (cons (name-of n) acc))
(define S (if suffix (~a (subformat q) " " suffix) (subformat q)))
(if (zero? r) S (~a S separator (number->words r))))
(cond
[(and (zero? n) (pair? acc)) (reverse acc)]
(cond [(< n 0) (~a "negative " (number->words (- n)))]
[(< n 20) (reverse (list (list-ref number-names n)))]
[(< n 20) (list-ref smalls n)]
[(< n 100) (let-values (([q r] (quotient/remainder n 10))) (n->list r (cons-name (* q 10))))]
[(< n 100) (step 10 #f "-" (curry list-ref tens))]
[(< n 1000) (step 100 "hundred" " ")]
[else (let*-values
[else (let loop ([N 1000000] [D 1000] [unit larges])
(([val] (argmax values (filter-map (compose (λ (val) (and (< val n) val)) car) numbers-alist)))
([q r] (quotient/remainder n val)))
(cond [(null? unit)
(n->list r (append (cons (name-of val) (reverse (n->list q null))) acc)))]))
(error 'number->words "number too big: ~e" n)]
[(< n N) (step D (car unit) " ")]
(string-join (n->list n null)))
[else (loop (* 1000 N) (* 1000 D) (cdr unit))]))]))

(define (first-cap s)
(define (first-cap s)
(string-append (string-upcase (substring s 0 1)) (substring s 1)))
(~a (string-upcase (substring s 0 1)) (substring s 1)))
(define (magic word [acc null])
(if (equal? word "four")
(string-join (reverse (cons "four is magic." acc)) ", \n")
(let* ([word-len (string-length word)]
[words (number->words word-len)])
(magic words
(cons (string-append word " is " words) acc)))))


(define (number-magic n)
(define (number-magic n)
(first-cap (magic (number->words n) null)))
(first-cap (magic (number->words n))))
(for ([n (append (range 11)
'(-10 23 172 20140 100 130 999999 876000000
874143425855745733896030))])
(displayln n)
(displayln (number-magic n))
(newline))
</lang>


{{out}}
(define (magic word accumulator)
(if (equal? word "four")
(string-join (reverse (cons "four is magic." accumulator)) ", ")
(let ((word-len (string-length word)))
(magic (number->words word-len)
(cons (string-append word " is " (number->words word-len)) accumulator)))))


<pre>
(module+ test
0
(define test-numbers
Zero is four,
(append (range 11) '(23 172 20140 100 130 876000000 874143425855745733896030)))
four is magic.
(for-each (λ (n) (displayln (number-magic n))) test-numbers))</lang>


1
{{out}}
One is three,
three is five,
five is four,
four is magic.


2
<pre>Zero is four, four is magic.
Two is three,
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,
Three is five, five is four, four is magic.
five is four,
four is magic.

3
Three is five,
five is four,
four is magic.

4
Four is magic.
Four is magic.

Five is four, four is magic.
5
Six is three, three is five, five is four, four is magic.
Seven is five, five is four, four is magic.
Five is four,
Eight is five, five is four, four is magic.
four is magic.

Nine is four, four is magic.
6
Ten is three, three is five, five is four, four is magic.
Six 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,
Twenty thousand one hundred forty is three, three is five, five is four, four is magic.
Ten is three, three is five, five is four, four is magic.
four is magic.

One hundred thirty is eighteen, eighteen is eight, eight is five, five is four, four is magic.
7
Six million is eleven, eleven is six, six is three, three is five, five is four, four is magic.
Seven is five,
Four sextillion three quintillion five quadrillion five trillion five billion three million six thousand thirty is eleven, eleven is six, six is three, three is five, five is four, four is magic.</pre>
five is four,
four is magic.

8
Eight is five,
five is four,
four is magic.

9
Nine is four,
four is magic.

10
Ten is three,
three is five,
five is four,
four is magic.

-10
Negative ten is twelve,
twelve is six,
six is three,
three is five,
five is four,
four is magic.

23
Twenty-three is twelve,
twelve is six,
six is three,
three is five,
five is four,
four is magic.

172
One hundred seventy-two is twenty-three,
twenty-three is twelve,
twelve is six,
six is three,
three is five,
five is four,
four is magic.

20140
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.

100
One hundred is eleven,
eleven is six,
six is three,
three is five,
five is four,
four is magic.

130
One hundred thirty is eighteen,
eighteen is eight,
eight is five,
five is four,
four is magic.

999999
Nine hundred ninety-nine thousand nine hundred ninety-nine is fifty-eight,
fifty-eight is eleven,
eleven is six,
six is three,
three is five,
five is four,
four is magic.

876000000
Eight hundred seventy-six million is thirty-three,
thirty-three is twelve,
twelve is six,
six is three,
three is five,
five is four,
four is magic.

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|REXX}}==
=={{header|REXX}}==