Problem 17
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.
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Numerics; namespace Euler { class Program { static string[] words = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; static string[] wordsTy = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; static string toWords(int n) { if (n <= 19) return words[n]; if (n <= 99) return wordsTy[n / 10] + words[n % 10]; if (n == 1000) return "onethousand"; string s; s = words[n / 100] + "hundred"; if (n % 100 > 0) s += "and" + toWords(n % 100); return s; } static void Main(string[] args) { Console.WriteLine(Enumerable.Range(1, 1000).Select(x => toWords(x).Length).Sum()); Console.ReadKey(); } } }
Racket
(define words
#(""
"one"
"two"
"three"
"four"
"five"
"six"
"seven"
"eight"
"nine"
"ten"
"eleven"
"twelve"
"thirteen"
"fourteen"
"fifteen"
"sixteen"
"seventeen"
"eighteen"
"nineteen"))
(define wordsTy
#(""
""
"twenty"
"thirty"
"forty"
"fifty"
"sixty"
"seventy"
"eighty"
"ninety"))
(define (toWords n)
(cond [(<= n 19) (vector-ref words n)]
[(<= n 99) (string-append (vector-ref wordsTy (quotient n 10)) (vector-ref words (modulo n 10)))]
[(= n 1000) "onethousand"]
[else
(let ([s (string-append (vector-ref words (quotient n 100)) "hundred")])
(when (> (modulo n 100) 0) (set! s (string-append s "and" (toWords (modulo n 100)))))
s)]))
(define (problem17)
(display (stream-fold + 0 (stream-map (lambda (x) (string-length (toWords x))) (in-range 1 1001)))))
(problem17)
Javascript
var words = ["",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"];
var wordsTy = [ "",
"",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"];
function toWords(n) {
if (n <= 19) return words[n];
if (n <= 99) return wordsTy[Math.floor(n / 10)] + words[n % 10];
if (n == 1000) return "onethousand";
var s;
s = words[Math.floor(n / 100)] + "hundred";
if (n % 100 > 0)
s += "and" + toWords(n % 100);
return s;
}
function problem17() {
var sum=0;
for (var x=1;x<=1000;x++) {
sum+=toWords(x).length;
}
alert(sum);
}
problem17();
Julia:
words = ["",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"]
wordsTy = ["",
"",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"]
function toWords(n)
if n <= 19 return words[begin + n] end
if n <= 99 return wordsTy[begin + Int(floor(n / 10))] * toWords(n % 10) end
if n == 1000 return "onethousand" end
s = words[begin + Int(floor(n / 100))] * "hundred"
if n % 100 > 0
s *= "and" * toWords(n % 100)
end
return s
end
function problem17()
sum=0
for x in 1:1000
sum += length(toWords(x))
end
print(sum)
end
problem17()
'Project Euler' 카테고리의 다른 글
Project Euler Problem 19 (0) | 2012.06.03 |
---|---|
Project Euler Problem 18 (0) | 2012.06.03 |
Project Euler Problem 16 (0) | 2012.06.03 |
Project Euler Problem 60 (0) | 2012.06.03 |
Project Euler Problem 59 (0) | 2012.05.29 |