Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Shouhong Wang, Hai Wang

1st Edition

1612330150, 978-1612330150

More Books

Students also viewed these Databases questions