# Daily Challenge #68

Alright, get ready for today’s challenge. This one is also an easy one.
You know how books are marked, well yeah, they have numbers that represent pages. Your task is to ask for a number of pages there are in a book and the program should return how many digits was used to mark the book.
Example:

``````digity(10) => 11 #Explanation: The pages are 1,2,3,4,5,6,7,8,9,10. If you count there are 11 digits :P
digity(785) => 2247 #This would take me a long time, so no explanation
``````

You know the deal,
Have fun!

3 Likes

Show Blocks

### Another logical method…

Show Blocks

This text will be hidden

1 Like

1 Like

hidden it now…

1 Like

Hi to all,
I have a solution.
Should I hide the blocks? If so how is that accomplished?

1 Like
My solution

Python:

``````n = int(input())
output = 0
for i in range(1,n+1):
output += len(str(i))
print(output)
``````

Kodular:

1 Like

A solution to count digits

Summary

This text will be blurred

Hi @nikif99
You can directly return result from`NepToLog10` procedure. There is no need to use do-result function.
Also it will be nice if you can wrap all blocks in a procedure.

2 Likes

Sure.
I prefered this way to avoid so wide blocks.

2 Likes

I really like it that @Mateja everytime when he posts a solution, he also shows it in Python. This is just amazing for understanding the blocks.

Yoshi

2 Likes

I always make the program in Python first, then convert it to Kodular.

1 Like

I cannot officially participate in this Daily Challenge anymore, and I’m aware of that. Still, I found the question elegantly mesmerizing. I have finally finished my own solution. Before I go on, I’d like to point out the fact that every solution in this thread so far uses iteration on the input to arrive on the answer. While this is an effective solution (and an easy one as well), it gets inefficient over larger inputs. Thus my single design goal was to achieve an answer without iterating on the input itself.

Here is what you all have done so far:

Iterating to the input
``````def digdig(pN):
digiNum = 0
for i in range(1, pN + 1):
digiNum = digiNum + len(str(i))
return(digiNum)
``````

Clearly, if the algorithm iterates n times for an input of length m, for any input of length m+1, the algorithm has to iterate approximately 10n times.

Here is what I did:

Iterating over the number of digits in the input
``````def dugdug(pN):
digiNum = 0
for i in range(1, len(str(pN)) + 1):
if i != len(str(pN)):
digiNum += (int(("9"+(i-1)*"0"))*i)
else:
digiNum += ((((int(pN))//(10**(i-1)))-1)*(10**(i-1))+((int(pN))%(10**(i-1))+1))*i
return(digiNum)
``````

In my solution, the algorithm iterates m times for an input of length m, which is much faster, especially with larger and larger inputs.

This optimization took me a relatively long time to achieve, but I’m mostly happy with the results. Do let me know what you guys think.

NOTE: `dugdug(12345678)` gives an interesting result. Evaluate it for yourself and see.

4 Likes

Marked as solution because it is the best way to achieve such result. Good job!

If anyone does not want to test:

``````dugdug(12345678) => 87654321
``````
3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.