Algorithm Question: Count zeros

Woodelin florveus
2 min readJun 21, 2021

--

Problem Statement

Don't think it's possible to count zeros, think again. During my algorithmic journey, I stumbled upon the counting zeros problem. The thought process was a bit hard for me to digest. I constantly thought about the integer ahead of zero, or the number between for example 2014. The problem states we need to count the number of zeros up to n which is the argument. For example, if n = 100 there should be about 11 zeros starting from 10 and upwards. However, up to 100, there is an extra zero at the end this counts toward the number of zeros you have which is 11.

Constraints and Challenges

The challenge and constraint with this problem are finding the best strategy to collect all zeros up to n. I came up with at least two solid strategies, however, they failed and did not work. It did not go through because of the extra zeros I did not account for, or because I was missing a logical expression to cut the work in half.

Process and strategy / Pseudo-Code

Below is my pseudocode to solve the problem. However, I’d advise you to take another stab at it before solving it.

Solution

I used brute force to solve this problem. I started by creating a counter variable and set it to zero. I used the for loop to go through the number. I increment the counter to the number then divided it by 10. The reason for this is logical, for example, if n was 50 there would be 5 zeros so the trick would be to divide by 10. Once I incremented the counter I used the Math. floor() and divided num by 10. This method is used to extract the largest number, for example, if the number does not divide by 10 evenly it will just take in the largest number. Now that we cut our work in half all we have to do is return it, here the solution below.

--

--

No responses yet