USE!!!PYTHON!!!! Write a recursive function base that has two parameters, n, a b
ID: 3668124 • Letter: U
Question
USE!!!PYTHON!!!!
Write a recursive function base that has two parameters, n, a base 10 positive integer, and b, an integer between 2 and 9. The function returns the base b representation of the number n. The base b representation of a number uses the digits 0,..,b-1 and the place of the digits indicate powers of the base. For example:
887 (base 10) = 2*7**3 + 4*7**2 + 0*7**1 + 5*7**0 = ‘2405’ (base 7)
Fortunately, this expansion means that is easy to compute using the % and // operators with respect to the base. For example, suppose that we want the base 7 representation of 887. Then, the last digit is ‘5’ because 887%7=5. And the preceding digits are ‘240’ because 887//7 = 126, and the base 7 representation of 126 is ‘240’. You may need to conduct additional research on base b representations.
Sample runs:
>>> base(0,3) # write 0 in base 3
'0'
>>> base(5,3) # write 5 in base 3
'12'
>>> base(887,7) # write 887 in base 7
'2405'
Explanation / Answer
02-recursive.py #!/usr/bin/env python import time; def recursive(base, exp): if exp == 0: return 1; if exp > 1: return base * recursive(base, exp - 1); else: return base; if __name__ == "__main__": print("Exponent Recursive"); for x in range(0, 10001, 1000): start = time.time(); recursive(3, x); end = time.time(); print("%i %g" % (x, end - start));
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.