Show problems as
Classic problem #50MediumMathRecursion

Pow(x, n)

Real-world version: Zoom Stage Multiplier
Tracked in this browser only
Read Pow(x, n) on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstBasic Recursive Functions · Recursion, animated step by step

A microscope has a motorised zoom stage. One click forward scales the magnification by factor, and one click backward undoes a forward click, so it divides the magnification by factor.

Given factor and a click count clicks, return the multiplier the stage applies overall. A positive clicks means that many clicks forward, a negative clicks means that many clicks backward, and zero clicks leave the magnification alone.

Report the multiplier rounded to 5 decimal places. The click count reaches into the billions, so a routine that turns the stage one click at a time is too slow: the number of multiplications you perform must grow with the logarithm of the click count, not with the count itself.

Examples

Example 1

Input
factor = 3.0, clicks = 4
Output
81.0

Four forward clicks multiply by 3 four times over, giving 81.

Example 2

Input
factor = 2.5, clicks = -3
Output
0.064

Three backward clicks divide by 2.5 three times, and 1 / 15.625 is 0.064.

Example 3

Input
factor = -1.5, clicks = 0
Output
1.0

The stage never moves, so the magnification is untouched and the multiplier is 1.

Constraints

  • -100.0 < factor < 100.0
  • -2^31 <= clicks <= 2^31 - 1
  • clicks is an integer
  • factor is non-zero, or clicks > 0
  • -10^4 <= the exact multiplier <= 10^4
  • The returned value is rounded to 5 decimal places

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def zoom_multiplier(factor: float, clicks: int) -> float:
Java
public double zoomMultiplier(double factor, int clicks)