2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 5900f37d1000cf542c50fe90
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								title: 'Problem 17: Number letter counts'
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 301804
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: problem-17-number-letter-counts
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								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 given `limit`  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.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`numberLetterCounts(5)`  should return a number. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(typeof numberLetterCounts(5) === 'number');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`numberLetterCounts(5)`  should return 19. 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.strictEqual(numberLetterCounts(5), 19);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`numberLetterCounts(150)`  should return 1903. 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.strictEqual(numberLetterCounts(150), 1903);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`numberLetterCounts(1000)`  should return 21124. 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.strictEqual(numberLetterCounts(1000), 21124);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-08-13 17:24:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function numberLetterCounts(limit) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								numberLetterCounts(5);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function numberLetterCounts(limit) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const dictionary = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    0: '',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    1: 'one',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    2: 'two',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    3: 'three',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    4: 'four',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    5: 'five',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    6: 'six',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    7: 'seven',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    8: 'eight',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    9: 'nine',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    10: 'ten',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    11: 'eleven',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    12: 'twelve',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    13: 'thirteen',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    14: 'fourteen',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    15: 'fifteen',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    16: 'sixteen',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    17: 'seventeen',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    18: 'eighteen',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    19: 'nineteen',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    20: 'twenty',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    30: 'thirty',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    40: 'forty',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    50: 'fifty',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    60: 'sixty',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    70: 'seventy',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    80: 'eighty',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    90: 'ninety',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    1000: 'onethousand'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  let numString = '';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  function convertToString(num) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // check dictionary for number
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (dictionary[num]) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return dictionary[num];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      const hundreds = Math.floor(num / 100);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      const tens =  Math.floor((num / 10) % 10) * 10;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      const remainder = num % 10;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      let tempStr = '';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (hundreds === 0) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        tempStr += dictionary[tens] + dictionary[remainder];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        tempStr += dictionary[hundreds] + 'hundred';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (tens !== 0 || remainder !== 0) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          tempStr += 'and';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (tens <  20 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          const lessThanTwenty = tens + remainder;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          tempStr += dictionary[lessThanTwenty];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          tempStr += dictionary[tens] + dictionary[remainder];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return tempStr;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  for (let i = 1; i < = limit; i++) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    numString += convertToString(i);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return numString.length;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```