Using function how to solve this question ,

WAP to calculate x to the power y , where x and y are two integer numbers entered by the user. [do not use pow() function]

Using function how to solve this question ,

WAP to calculate x to the power y , where x and y are two integer numbers entered by the user. [do not use pow() function]

Use binary exponentation algorithm to calculate a^b

1 Like

But how

I am beginner

Search and read the algo. , it is a recursive algorithm in which we keep on dividing the pow

like 2^n=2^(n/2)*2^(n/2) until the power becomes 0 (base case).

commplexity–>O(logn)

2 Likes

what’s wrong with a simple x**y ?

```
>>> x,y = map(int , input().split());print(x**y)
>>> 3 4
81
```

sorry if i interpreted the question wrong cause other comments look not-so-simple tbh

1 Like

you mean `to the power`

right? cuz that would give XOR otherwise @shubhamkumar68

I mean you must be knowing this but since you said

that^^^ i just wanted to avoid any confusion

@shubhamkumar68

long long power(long long x, int n) {

long long result = 1;

```
while (n) {
if (n & 1)result *= x; //check whether n is odd
x *= x;
n = n >> 1;
}
return result;
```

}

you should go through this link for better understanding

1 Like

Too slow

1 Like

then use pow in python. [if u really wana fast function]

```
pow(a,b,m)
```

in python works fast.

this will return (a**b) % m, right?

YES