Posted By

chroto on 06/08/10


Tagged

project Euler


Versions (?)

Project Euler - Problem 17


 / Published in: Python
 

  1. #!/usr/bin/python
  2.  
  3. # Project Euler - Problem 17
  4. # In the numbers 1 - 1000 how many letters are needed
  5. # to spell out each word.
  6.  
  7. sum = 11 # one thousand
  8. for n in range(1,1000):
  9.  
  10. if n % 900 != n:
  11. sum += 14 # 'nine hundred and'
  12. x = n % 900
  13. elif n % 800 != n:
  14. sum += 15 # 'eight hundred and'
  15. x = n % 800
  16. elif n % 700 != n:
  17. sum += 15 # 'seven hundred and'
  18. x = n % 700
  19. elif n % 600 != n:
  20. sum += 13 # 'six hundred and'
  21. x = n % 600
  22. elif n % 500 != n:
  23. sum += 14 # 'five hundred and'
  24. x = n % 500
  25. elif n % 400 != n:
  26. sum += 14 # 'four hundred and'
  27. x = n % 400
  28. elif n % 300 != n:
  29. sum += 15 # 'three hundred and'
  30. x = n % 300
  31. elif n % 200 != n:
  32. sum += 13 # 'two hundred and'
  33. x = n % 200
  34. elif n % 100 != n:
  35. sum += 13 # 'one hundred and'
  36. x = n % 100
  37. else:
  38. x = n # n < 100
  39.  
  40. if x == 0:
  41. sum -= 3 # 'remove "and"'
  42. elif x % 90 != x:
  43. sum += 6 # 'ninety'
  44. x = x % 90
  45. elif x % 80 != x:
  46. sum += 6 # 'eighty'
  47. x = x % 80
  48. elif x % 70 != x:
  49. sum += 7 # 'seventy'
  50. x = x % 70
  51. elif x % 60 != x:
  52. sum += 5 # 'sixty'
  53. x = x % 60
  54. elif x % 50 != x:
  55. sum += 5 # 'fifty'
  56. x = x % 50
  57. elif x % 40 != x:
  58. sum += 5 # 'forty'
  59. x = x % 40
  60. elif x % 30 != x:
  61. sum += 6 # 'thirty'
  62. x = x % 30
  63. elif x % 20 != x:
  64. sum += 6 # 'twenty'
  65. x = x % 20
  66. elif x % 10 != x:
  67. if x == 17:
  68. sum += 9
  69. elif x == 11 or x == 12:
  70. sum += 6
  71. elif x == 15 or x == 16:
  72. sum += 7
  73. elif x == 10:
  74. sum += 3
  75. else:
  76. sum += 8
  77. x = 0
  78.  
  79. if x == 1 or x == 2 or x == 6:
  80. sum += 3
  81. elif x == 3 or x == 7 or x== 8:
  82. sum += 5
  83. elif x == 4 or x == 5 or x == 9:
  84. sum += 4
  85.  
  86.  
  87. print sum

Report this snippet  

You need to login to post a comment.