Your question is Writing Power Recursively and Iteratively. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Brady software may repeatedly calculate scaling factors for label layouts and print configurations. Implement exponentiation for a number and an integer exponent using both recursive and non-recursive approaches, and avoid multiplying once per exponent value.
Return base^exponent using exponentiation by squaring. Your implementation must support positive, zero, and negative exponents. Provide these functions:
power_recursive(base, exponent), using recursive exponentiation by squaring.power_iterative(base, exponent), using an iterative binary-exponentiation loop.power(base, exponent), which returns the result using your recursive implementation.For negative exponents, return the reciprocal of the corresponding positive power. Inputs are chosen so that the result is exactly representable for integer exponents, or normal Python floating-point behavior is acceptable for negative exponents.
base, an integer, and exponent, an integer.base^exponent. Positive exponents and zero should return an integer when mathematically integral; negative exponents may return a float.base == 0 with a negative exponent will not appear.def power(base, exponent):