Question: write a Python program that implements this (or a similar) algorithm efficiently and correctly calculates the number of zeros written from one to one million.

write a Python program that implements this (or a similar) algorithm efficiently and correctly calculates the number of zeros written from one to one million. appropriately comment your source code as necessary

: n 0

count 0

repeat

n n + 1

count count + the number of zeros in n

until n is 1 million

display count

Of course, how can the number of zeros in n be counted? An algorithm for this could be:

zeros 0

repeat

if n % 10 is 0

then

zeros zeros + 1

end

n n / 10

until n is 0

This algorithm checks to see if a remainder exists when n is divided by 10 (i.e., the value of the rightmost digit of n). If there is no remainder, then the right-most digit must be 0, and the counter is incremented. The number is then divided by ten (integer division) to remove the right-most digit, and the process continues until n is 0. Take, for example, the number 10,102:

n remainder quotient

10,102 2 1,010

1,010 0 101

101 1 10

10 0 1

1 1 0

#zeros 2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!