Skip to content
Advertisement

Project Euler Problem 17 Python

Please let me know how to bug fix this code . I tried and correct a lot of things , but I am 10 extra to the solution !

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.

My solution

JavaScript

I am getting 21134 and the answer is… (SPOILER: please hover over next line to view)

21124

VERY ANNOYING !

Advertisement

Answer

The word “eighteen” only has eight letters, not nine. Since it appears ten times in the range 1-1000, that would explain the discrepancy.

By the way, if you’re checking if n is less than 10, why not simply use n<10 instead of n/10 == 0?

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement