Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions C++/Program-15/program.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

/*
* Code for modular exponentiation
* Time complexity : O(n*log(n))
*/

#include<bits/stdc++.h>
using namespace std;

// This function returns value of (x^y)%mod.
int power(int x, int y, int mod)
{
int answer = 1; // Initialise the answer

x = x % mod;

if(x == 0) return 0; // If x is divisible by mod

while(y > 0)
{
if(y % 2 == 1) // If y is odd multiply x with answer
{
answer = (answer*x) % mod;
}

y /= 2; // reduce the value of y
x = (x*x) % mod; // increment the value of x
}

return answer;
}


// Main function
int main()
{
int x = 2, y = 10, mod = 1000000007;
int result = power(x, y, mod);
cout << result << '\n';

return 0;
}
1 change: 1 addition & 0 deletions C++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
| Program-08 | Program to reverse a string |
| Program-09 | Program to check if two numbers are equal without using arithmetic operators or comparison operators.
| Program-10 | Program to find the missing number in a Sorted Array.
| Program-15 | Program to find modular exponentiation.