mediumNeetCode 150
Pow(x, n)
Calculate x raised to the power n Example: x = 2.0, n = 10 => 1024.0 Example: x = 2.0, n = -2 => 0.25
I square x and halve n each step, so the loop runs log n times. A negative n flips the answer to one over the result.
Math & Geometry
Examples
You write
my_pow(x: float, n: int)
Example 1
- Input
- x = 2.0, n = 10
- returns
- 1024.0
Example 2
- Input
- x = 3.0, n = 3
- returns
- 27.0

Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
1Pow(x, n)medium
Based onLeetCode (opens in a new tab)