Problem 17¶
Number Letter Counts¶
If the numbers $1$ to $5$ are written out in words: one, two, three, four, five, then there are $3 + 3 + 5 + 4 + 4 = 19$ letters used in total.
If all the numbers from $1$ to $1000$ (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, $342$ (three hundred and forty-two) contains $23$ letters and $115$ (one hundred and fifteen) contains $20$ letters. The use of "and" when writing out numbers is in compliance with British usage.
数字の文字数¶
1 から 5 までの数字を英単語で書けば one, two, three, four, five であり, 全部で 3 + 3 + 5 + 4 + 4 = 19 の文字が使われている.
では 1 から 1000 (one thousand) までの数字をすべて英単語で書けば, 全部で何文字になるか.
注: 空白文字やハイフンを数えないこと. 例えば, 342 (three hundred and forty-two) は 23 文字, 115 (one hundred and fifteen) は20文字と数える. なお, "and" を使用するのは英国の慣習.
#!r6rs
#!chezscheme
(import (chezscheme))
(define (number-letter num)
(assert (and (<= 1 num) (<= num 1000)))
(case num
[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"]
[1000 "one thousand"]
[else
(if (< num 100)
; 2桁(10の位と1の位に分けて処理)
(let-values ([(upper lower) (div-and-mod num 10)])
(format "~D-~D" (number-letter (* upper 10)) (number-letter lower)))
; 3桁(100の位と残り2桁に分けて処理)
(let-values ([(upper lower) (div-and-mod num 100)])
(if (zero? lower)
(format "~D hundred" (number-letter upper))
(format "~D hundred and ~D"
(number-letter upper)
(number-letter lower)))))]))
(define all-number-letters
(map number-letter (cdr (iota 1001))))
(define concatenate-all-number-letters
(fold-left
(lambda (result letter)
(string-append result letter))
""
all-number-letters))
(define (erase-space-hyphen str)
(list->string
(filter
(lambda (char)
(case char
[(#\space #\-) #f]
[else #t]))
(string->list str))))
(define answer-17
(string-length (erase-space-hyphen concatenate-all-number-letters)))
(printf "17: ~D~%" answer-17)
Noppi が2023/12/31に更新 · 2件の履歴