From de94bab52e608320e21ee1b6f4a6c39a9edf9006 Mon Sep 17 00:00:00 2001 From: Utkarsh Prakhar <95642114+Utkarsh048@users.noreply.github.com> Date: Wed, 29 Nov 2023 15:48:04 +0530 Subject: [PATCH] Create Pow(x, n).py --- Pow(x, n).py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Pow(x, n).py diff --git a/Pow(x, n).py b/Pow(x, n).py new file mode 100644 index 000000000..9248516d6 --- /dev/null +++ b/Pow(x, n).py @@ -0,0 +1,18 @@ +class Solution(object): + def myPow(self, x, n): + if n == 0: + return 1.0 + if n < 0: + x = 1 / x + n = -n + + def power(x, n): + if n == 0: + return 1.0 + half = power(x, n // 2) + if n % 2 == 0: + return half * half + else: + return half * half * x + + return power(x, n)