Project Euler : Problem 017

Project Euler problems in Oforth . Feel free to post your own code.

Project Euler : Problem 017

Postby Franck » 02 Aug 2015 22:40

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.

Code: Select all
[ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]
[ "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"] +
Constant new: UNIT

["ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" ] Constant new: TENTH

Integer method: asName
{
   self   20 <  ifTrue: [ UNIT at(self) return ]
   self  100 <  ifTrue: [ self 10 /mod TENTH at swap asName + return ]
   self 1000 == ifTrue: [ "onethousand" return ]
   self isMultiple(100) ifTrue: [ UNIT at(self 100 / ) "hundred" + return ]
   self 100 /mod UNIT at "hundredand" + swap asName +
}

: pe017 { 0 #[ asName size + ] 1000 seqEach }
Franck
 
Posts: 140
Joined: 29 Oct 2014 19:01

Return to Project Euler in Oforth

Who is online

Users browsing this forum: No registered users and 1 guest

cron