/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/python # Project Euler - Problem 17 # In the numbers 1 - 1000 how many letters are needed # to spell out each word. sum = 11 # one thousand for n in range(1,1000): if n % 900 != n: sum += 14 # 'nine hundred and' x = n % 900 elif n % 800 != n: sum += 15 # 'eight hundred and' x = n % 800 elif n % 700 != n: sum += 15 # 'seven hundred and' x = n % 700 elif n % 600 != n: sum += 13 # 'six hundred and' x = n % 600 elif n % 500 != n: sum += 14 # 'five hundred and' x = n % 500 elif n % 400 != n: sum += 14 # 'four hundred and' x = n % 400 elif n % 300 != n: sum += 15 # 'three hundred and' x = n % 300 elif n % 200 != n: sum += 13 # 'two hundred and' x = n % 200 elif n % 100 != n: sum += 13 # 'one hundred and' x = n % 100 else: x = n # n < 100 if x == 0: sum -= 3 # 'remove "and"' elif x % 90 != x: sum += 6 # 'ninety' x = x % 90 elif x % 80 != x: sum += 6 # 'eighty' x = x % 80 elif x % 70 != x: sum += 7 # 'seventy' x = x % 70 elif x % 60 != x: sum += 5 # 'sixty' x = x % 60 elif x % 50 != x: sum += 5 # 'fifty' x = x % 50 elif x % 40 != x: sum += 5 # 'forty' x = x % 40 elif x % 30 != x: sum += 6 # 'thirty' x = x % 30 elif x % 20 != x: sum += 6 # 'twenty' x = x % 20 elif x % 10 != x: if x == 17: sum += 9 elif x == 11 or x == 12: sum += 6 elif x == 15 or x == 16: sum += 7 elif x == 10: sum += 3 else: sum += 8 x = 0 if x == 1 or x == 2 or x == 6: sum += 3 elif x == 3 or x == 7 or x== 8: sum += 5 elif x == 4 or x == 5 or x == 9: sum += 4 print sum