Advertisement
makispaiktis

Project Euler 17 - Number letter counts

Aug 14th, 2020 (edited)
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. '''
  2. 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
  3. used in total.
  4. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
  5. NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115
  6. (one hundred and fifteen) contains 20 letters.
  7. The use of "and" when writing out numbers is in compliance with British usage.
  8. '''
  9.  
  10. units = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
  11. string2 = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
  12. decs = ["ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
  13. thousand = "onethousand"
  14. AND = 3
  15. HUNDRED = 7
  16.  
  17. # 1. From 1 to 9
  18. sum1 = sum(len(units[k]) for k in range(len(units)))
  19. print("sum1 = " + str(sum1))
  20.  
  21. # 2. Only ten
  22. sum2 = len(decs[0])
  23. print("sum2 = " + str(sum2))
  24.  
  25. # 3. From 11 to 19
  26. sum3 = sum(len(string2[k]) for k in range(len(string2)))
  27. print("sum3 = " + str(sum3))
  28.  
  29. # 4. From 20 to 99
  30. sum4 = 0
  31. for i in range(1, len(decs)):
  32.     sum4 += 10*len(decs[i])             # I use 10 times the words "twenty", "thirty", "forty", ......
  33. for j in range(len(units)):
  34.     sum4 += 8*len(units[j])             # I use 8 times the words "one", "two", "three" when counting from 20 to 99
  35. print("sum4 = " + str(sum4))            # Example: 21, 31, 41, 51, 61, 71, 81, 91 ----> I use 8 times the "one"
  36.  
  37. # SUM1TO99
  38. sum1to99 = sum1 + sum2 + sum3 + sum4
  39. print("sum1to99 = " + str(sum1to99))
  40.  
  41. # 5. From 100 to 999, there are exactly 900 numbers
  42. # 5-a: Counting "and"
  43. counterAND = (900 - 9) * AND
  44. # 900 = all the numbers from 100 to 999
  45. # 9 = the numbers from 100 to 999 where I dont use "the and-word" ---> 100, 200, ...., 800, 900
  46.  
  47. # 5-b: Counting "hundred"
  48. counterHUNDRED = 900 * HUNDRED
  49.  
  50. # 5-c: Now, its time to count the real numbers (without "and" and "hundred")
  51. # I will use 9 more times the sum1to99
  52. counter1 = 9 * sum1to99
  53.  
  54. # 5-d: Last task is to count the "one", "two", .... before the word "HUNDRED"
  55. counter2 = 0
  56. for i in range(len(units)):
  57.     counter2 += 100*len(units[i])       # "One" is used 100 times from 100 to 199, "two" is used 100 times from 200 to 299
  58.  
  59. sum100to999 = counter1 + counter2 + counterAND + counterHUNDRED
  60. print("sum100to999 = " + str(sum100to999))
  61.  
  62. # 6. "1000"
  63. thous = len(thousand)
  64. print("thous = " + str(thous))
  65.  
  66. # MAIN FUNCTION
  67. sumALL = sum1to99 + sum100to999 + thous
  68. print()
  69. print("SUM = " + str(sumALL))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement