Skip to content

Commit

Permalink
Mathematics: Addition of code for finding Power of any 2 numbers
Browse files Browse the repository at this point in the history
I've added code for finding power of any 2 numbers.
Its written in C++ and the numbers can be long long int where 1e9+7 modulo
takes care of any overflow.

Signed-off-by: Kalpesh Pandya <[email protected]>
  • Loading branch information
kapandya committed Oct 14, 2022
1 parent 0d02aae commit f2aac0f
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Mathematics/Power_of_any_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
PROBLEM STATEMENT:
Given 2 numbers x and y, you need to find x raise to y.
*/

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define FIO ios::sync_with_stdio(0);
ll power(ll a,ll b)
{
ll res=1;
while(b!=0)
{
if(b%2!=0)
{
res=(res*a)%1000000007;
b--;
}
else
{
a=(a*a)%1000000007;
b/=2;
}
}
return res;
}
int main()
{
FIO;
ll number_a,number_b,answer;
cin >> number_a >> number_b;
answer = power(number_a,number_b);
cout << answer;
return 0;
}

0 comments on commit f2aac0f

Please sign in to comment.