-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
modular_exponentiation.cpp
71 lines (61 loc) · 1.36 KB
/
modular_exponentiation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<bits/stdc++.h>
using namespace std;
/*
Normal power algorithm would take O(power) time to calculate the answer.
But here we use the fact that every number can be represented in power of 2!
Consider 3^25
Normal calculation = (3*3*... 14 Times)
Modular exponentiation = (3^8)*(3^4)*(3^2)*(3^1)
We use this power of 2s to our advantage.
(3^2)=(3^1)^2
(3^4)=(3^2)^2
(3^8)=(3^4)^2
So, this reduces the number of multiplication, as we only need to sqaure the number (squaring is just single multiplication operation).
*/
long long modular_exponentiation(long long base,long long power,long long mod)
{
base=base%mod;
long long answer=1;
while(power>0)
{
if(power%2==1)
{
answer=(answer*base)%mod;
}
base=(base*base)%mod;
power=power/2;
}
return answer;
}
int main()
{
long long base,power,mod;
cout<<"Enter base: ";
cin>>base;
cout<<"Enter power: ";
cin>>power;
cout<<"Enter mod: ";
cin>>mod;
cout<<"(power^base)%mod = "<<modular_exponentiation(base,power,mod)<<"\n";
}
/*
Sample I/0
1.
INPUT
Enter base: 2
Enter power: 10
Enter mod: 8000
OUTPUT
(power^base)%mod = 1024
2.
INPUT
Enter base: 3
Enter power: 6
Enter mod: 1
OUTPUT
(power^base)%mod = 0
*/
/*
Time Complexity: O(log(power))
Space Complexity: O(1)
*/