Powers

Save both functions into the file powers.py.

  1. Write the function powers_1() that takes two positive integers, x and maxn, and returns the average of the sequence of powers of x less than or equal to maxn. Examples:

    >>> round(powers_1 (2, 100), 2)
    18.14
    >>> round(powers_1 (3, 200), 2)
    24.2
    >>> round(powers_1 (5, 100), 2)
    10.33
    >>> round(powers_1 (7, 1000), 2)
    100.0
    

    Note

    More tests are provided in file powers-1.txt

  2. Write the function powers_2() that takes two positive integers, x and maxn, and another integer, d, such that 0<=d<10, and returns the first number of the sequence of powers of x less than or equal to maxn such that its digit in the units position is d. If there is no such number, this function must return -1. Examples:

    >>> powers_2 (2, 100, 6)
    16
    >>> powers_2 (2, 100, 5)
    -1
    >>> powers_2 (5, 100, 5)
    5
    >>> powers_2 (5, 100, 3)
    -1
    >>> powers_2 (7, 1000, 3)
    343
    

    Note

    More tests are provided in file powers-2.txt

Solution

A solution is provided in file powers.py