the-honk/daily-programmer/#390 - Number of 1s.py
2024-10-09 18:02:36 +01:00

16 lines
334 B
Python

def count1(n):
x = str(n)
count = 0
for index, digit in enumerate(x[::-1]):
digit = int(digit)
if digit != 0:
if digit == 1:
numberAfter = x[len(x) - index:] or '0'
count += int(numberAfter) + 1
else:
count += 10 ** index
count += int(10 ** (index - 1) * index * digit)
return count
print(count1(3**35))