Why does looping over the digits of n cost only log n, and why doesn't stopping a search early change its worst case?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
The video showed that taking the last digit of n is the same as dividing by 10 and keeping the remainder: 4271 gives remainder 1 and leaves 427. Write digit_sum(n) for an int n >= 0 that adds up the digits of n without converting it to a string, peeling off one digit per pass of a while loop. You may find % and // useful. Define digit_sum(n).