|
| 1 | +""" |
| 2 | +Iterative solution to calculate the power of a base raised to an exponent. |
| 3 | +
|
| 4 | +This implementation uses an iterative approach, unlike the recursive approach |
| 5 | +in `power_using_recursion.py`. The algorithm is based on exponentiation by squaring |
| 6 | +for optimal performance. |
| 7 | +
|
| 8 | +Examples: |
| 9 | + >>> power(2, 3) |
| 10 | + 8 |
| 11 | + >>> power(5, -2) |
| 12 | + 0.04 |
| 13 | + >>> power(10, 0) |
| 14 | + 1 |
| 15 | + >>> Failed example: |
| 16 | + power(5, -2) |
| 17 | + Expected: |
| 18 | + 0.04 |
| 19 | + Got: |
| 20 | + 0.04000000000000001 |
| 21 | + 1 items had failures: |
| 22 | + 1 of 3 in __main__ |
| 23 | + 1 failed in total |
| 24 | + Raise base to the power of exponent using an optimized approach... |
| 25 | + Enter the base: 500 |
| 26 | + Enter the exponent: 8 |
| 27 | + 500.0 to the power of 8 is 3.90625e+21 |
| 28 | +
|
| 29 | +Input: |
| 30 | + base (float): The base number (can be integer or float). |
| 31 | + exponent (int): The exponent (can be positive, negative, or zero). |
| 32 | +
|
| 33 | +Output: |
| 34 | + float: The result of base raised to the power of exponent. |
| 35 | +
|
| 36 | +Note: |
| 37 | + Results for very large or very small floating-point numbers may have slight precision errors |
| 38 | + due to the limitations of floating-point arithmetic. |
| 39 | +""" |
| 40 | + |
| 41 | + |
| 42 | +def power(base: float, exponent: int) -> float: |
| 43 | + """ |
| 44 | + Optimized power function using exponentiation by squaring. |
| 45 | + It handles both positive and negative exponents efficiently. |
| 46 | + This function take time complexity O(log n) for exponentiation. |
| 47 | + space complexity is O(1) as it uses a constant amount of space. |
| 48 | + """ |
| 49 | + # Handle negative exponents by taking reciprocal of the base |
| 50 | + if exponent < 0: |
| 51 | + base = 1 / base |
| 52 | + exponent = -exponent |
| 53 | + |
| 54 | + result = 1 |
| 55 | + # Use exponentiation by squaring for efficiency |
| 56 | + while exponent: |
| 57 | + if exponent % 2 == 1: # If the current exponent is odd |
| 58 | + result *= base |
| 59 | + base *= base # Square the base |
| 60 | + exponent //= 2 # Halve the exponent |
| 61 | + return result |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + import doctest |
| 66 | + |
| 67 | + doctest.testmod() |
| 68 | + print("Raise base to the power of exponent using an optimized approach...") |
| 69 | + |
| 70 | + try: |
| 71 | + # Input handling and validation |
| 72 | + base = float(input("Enter the base: ").strip()) # Supports float & int |
| 73 | + exponent = int( |
| 74 | + input("Enter the exponent: ").strip() |
| 75 | + ) # Ensures exponent is an integer |
| 76 | + |
| 77 | + # Calculate result |
| 78 | + result = power(base, exponent) |
| 79 | + |
| 80 | + # Display the result |
| 81 | + print(f"{base} to the power of {exponent} is {result}") |
| 82 | + |
| 83 | + except ValueError: |
| 84 | + # Handle invalid input |
| 85 | + print("Invalid input! Please enter numeric values for base and exponent.") |
0 commit comments