/*********************************************************
http://projecteuler.net/problem=17
Euler #17 in Picat.
"""
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.
"""
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*********************************************************/
main => go.
go => euler17.
english(N) = English =>
Divs = [1000000000, 1000000, 1000, 100],
Divnames = ["billion", "million", "thousand", "hundred"],
Prefixes = ["0", "twen", "thir", "for", "fif", "six", "seven", "eigh", "nine"],
_Ordinals = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh",
"eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth",
"fourteenth","fifteenth", "sixteenth", "seventeenth",
"eighteenth", "nineteenth"],
Cardinals = ["one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"],
Sstr = "",
Printed = 0,
if N < 0 then
Sstr := "minus" ++ Sstr,
N := -N
end,
foreach(I in 1..Divs.length)
D = N div Divs[I],
N := N mod Divs[I],
if D != 0 then
Sstr := Sstr ++ english(D) ++ Divnames[I],
Printed := 1
end
end,
if N > 0, Printed = 1 then
Sstr := Sstr ++ "and"
end,
if N == 0 then
1 == 1 % dummy
elseif N > 19 then
D = N div 10,
N := N mod 10,
Sstr := Sstr ++ Prefixes[D] ++ "ty" ++ english(N)
else
Sstr := Sstr ++ Cardinals[N]
end,
English = Sstr.
euler17 =>
println("Problem 17:"),
Total = 0,
foreach(I in 1..1000)
Sstr = english(I),
Total := Total + Sstr.length
end,
println(Total),
nl.