0
\$\begingroup\$

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.

Output how many letters would be used if all the numbers from 1 to 1000 (one thousand) inclusive were written out in words.

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.

You must actually calculate it - just printing the predetermined result is not allowed.

Output with any acceptable method.

\$\endgroup\$

closed as off-topic by Arnauld, Luis felipe De jesus Munoz, Stephen, ASCII-only, Quintec Nov 16 '18 at 0:22

  • This question does not appear to be about programming puzzles or code golf within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Probable duplicate of this question \$\endgroup\$ – Giuseppe Nov 15 '18 at 22:02
  • 7
    \$\begingroup\$ Welcome to PPCG! To prevent hardcoding, I'd strongly recommend to either 1) take a number \$1\le N \le 1000\$ as input and ask the number of letters used to write \$1\$ to \$N\$ or 2) take 2 numbers \$A\$ and \$B\$ as input and ask the number of letters used to write \$A\$ to \$B\$. \$\endgroup\$ – Arnauld Nov 15 '18 at 22:02
  • 2
    \$\begingroup\$ This would be better as "output how many letters are used if the numbers 1 through n were written out" to avoid any question of whether something prints a predetermined result. But I think that's a duplicate. \$\endgroup\$ – lirtosiast Nov 15 '18 at 22:04
  • 3
    \$\begingroup\$ It turns out that this is a slightly edited copy of problem 17 from Project Euler. Posting such problems here is strongly discouraged, so it should probably be closed and/or deleted. \$\endgroup\$ – Arnauld Nov 15 '18 at 23:30
  • 10
    \$\begingroup\$ I'm voting to close this question as off-topic because this challenge was taken from another site without any reference or authorization. \$\endgroup\$ – Arnauld Nov 15 '18 at 23:32
2
\$\begingroup\$

Mathematica, 38 bytes

StringLength@StringJoin@IntegerName[Range[1000]]

@Doorknob has a leaner implementation:

Tr@StringLength@IntegerName@Range@1000

(* 20961 *)


Explanation

IntegerName[Range[10]]

(* {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"} *)

StringJoin@IntegerName[Range[10]]

(* "onetwothreefourfivesixseveneightnineten" *)

StringLength@StringJoin@IntegerName[Range[10]]

(* 39 *)

\$\endgroup\$
  • \$\begingroup\$ Since StringLength threads over lists (and you can use prefix notation for all of these functions), this can be shortened to Tr@StringLength@IntegerName@Range@1000 to save 10 bytes. \$\endgroup\$ – Doorknob Nov 15 '18 at 23:50
  • \$\begingroup\$ @Doorknob: Oooh... yes. Nice. Thanks. \$\endgroup\$ – David G. Stork Nov 15 '18 at 23:51
  • \$\begingroup\$ The expected result is 21124. The gap is prime (21124 - 20961 = 163), so it's not \$N\$ times the same difference. I wonder where it comes from. \$\endgroup\$ – Arnauld Nov 16 '18 at 15:22
  • \$\begingroup\$ When I delete white spaces I get 19169. \$\endgroup\$ – David G. Stork Nov 17 '18 at 1:03
  • \$\begingroup\$ Are you also deleting hyphens? And are the numbers formatted the UK way? (three hundred and forty-two)? \$\endgroup\$ – Arnauld Nov 19 '18 at 16:06
1
\$\begingroup\$

Common Lisp, 104 98 85 76 75 74 bytes

(princ(loop for i to 999 sum(count-if'both-case-p(format nil"~R"(1+ i)))))

Try it online!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.