Hi I'm participating in a math competition and there is this question " N = 9+99+999+... (a number that has 999 nines), how many are there 1 in N. So I did the question mathematically and it said that there are 998 ones in N (and that is the correct answer) but when I made a python program to solve this question:
N = 0
nr_of_ones = 0
for i in range(1, 1000):
N += 10**i - 1
length_N = len(str(N))
N = str(N)
print("N =", N)
print("Length of N =", length_N)
for x in range(length_N):
if N[x] == 1:
nr_of_ones += 1
print(nr_of_ones)
but the result that i got was false, it said that the length of N = 1000 and the nr_of_ones = 999 (wich is false). If anyone could help me correct my code I would be very grateful.
I've tried to rewrite it countless times, to use the package math, and to use golang but none of it worked, it should display that the number of ones = 998 but it says it's 999