Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.

Commit 99fe7f9

Browse files
authored
Merge pull request #33 from roopbiswas/patch1
Create modular_exponentiation.cpp
2 parents 20c7f30 + 99fcf49 commit 99fe7f9

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

math/modular_exponentiation.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// modular exponentiation implemented in C++
2+
//Author : Rituparno Biswas
3+
4+
//Given three numbers x, y and p, compute (power(x,y)) % p. where power(a,b) calculates a to power of b
5+
6+
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
10+
typedef long long int ll;
11+
12+
ll power(ll x, ll y, ll p)
13+
{
14+
if(x==0)
15+
return 0;
16+
if(y==0)
17+
return 1;
18+
if (y%2==0)
19+
{
20+
ll g=power(x,y/2,p);
21+
return (g*g)%p;
22+
}
23+
else
24+
{
25+
ll g=power(x,y/2,p);
26+
return (x*(g*g)%p)%p;
27+
}
28+
}
29+
30+
int main()
31+
{
32+
ll x = 2,y = 5, p = 13;
33+
printf("Power is %lld", power(x, y, p));
34+
return 0;
35+
}

0 commit comments

Comments
 (0)