A Simple Problem in Number
CSCI 1470

by K. Yue

1. Problem Goal

Question: find the smallest natural number p such that 10**p >= i, where i is a non-negative integer.

Complete the following Python shell program. Algorithms are provided in the documentation.

num_digits_shell_1.py:

#
#   Question: find the smallest natural number p such that 10**p >= i, where i is a non-negative integer.
#
def num_digit_1(i: int) -> int:
    """ Algorithm:
        [1] result <- 1 (a non-negative number has at least one digit.
        [2] dividend <-- i // 10
        [3] while divident > 0:
        [3.1]     result <- result + 1
        [3.2]     divident <-- dividend // 10
        [4] return result
    """
    pass
   
print(f"num_digit_1(0): {num_digit_1(0)}")
print(f"num_digit_1(5): {num_digit_1(5)}")
print(f"num_digit_1(1000): {num_digit_1(1000)}")
print(f"num_digit_1(1234567890): {num_digit_1(1234567890)}")

import math
def num_digit_2(i: int) -> int:
    """ Algorithm:
        if i == 0:
            return 1
        else:
            return floor(log(i)) + 1
    """
    pass

print(f"num_digit_2(0): {num_digit_2(0)}")
print(f"num_digit_2(5): {num_digit_2(5)}")
print(f"num_digit_2(1000): {num_digit_2(2000)}")
print(f"num_digit_2(1234567890): {num_digit_2(1234567890)}")

def num_digit_3(i: int) -> int:
    """
        Another version.
    """
    pass

print(f"num_digit_3(0): {num_digit_3(0)}")
print(f"num_digit_3(5): {num_digit_3(5)}")
print(f"num_digit_3(1000): {num_digit_3(2000)}")
print(f"num_digit_3(1234567890): {num_digit_3(1234567890)}")