Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast Exponentiation Algorithms Added #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions src/main/cpp/algorithms/math/Binary_Exponentiation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<bits/stdc++.h>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a meaningful header and also some informative comments to different parts of the code to be more readable.

#define ll long long

using namespace std;

ll power(ll a,ll n)
{
ll res=1;

while(n)
{
if(n&1)
{
res=res*a;
n--;
}
a*=a;
n/=2;
}
return res;
}

int main()
{
ll a,n;
cin>>a>>n;
cout<<power(a,n);
return 0;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you possibly implement the unit test for this algorithm and put it in the corresponding C++ unit test folder?

80 changes: 80 additions & 0 deletions src/main/cpp/algorithms/math/Matrix_exponentiation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//for more info refer : https://www.spoj.com/problems/MPOW/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a meaningful header and also some informative comments to different parts of the code to be more readable.

#include<bits/stdc++.h>
#define ll long long
#define MOD 1000000007
using namespace std;
#define N 51
ll mat[51][51],I[51][51];
void show(ll A[][N],int dim)
{
for(int i=0;i<dim;i++)
{
for(int j=0;j<dim;j++)
cout<<A[i][j]<<" ";
cout<<"\n";
}

}
void mul(ll A[][N],ll B[][N],int dim)
{
ll res[dim][dim];
for(int i=0;i<dim;i++)
{

for(int j=0;j<dim;j++)
{
res[i][j]=0;
for(int k=0;k<dim;k++)
res[i][j]=(res[i][j]%MOD+(A[i][k]%MOD * B[k][j]%MOD)% MOD)%MOD;
}
}
for(int i=0;i<dim;i++)
{
for(int j=0;j<dim;j++)
{
A[i][j]=res[i][j];
}
}
}
void power(ll A[][N],int dim,int n)
{
for(int i=0;i<dim;i++)
{
for(int j=0;j<dim;j++)
if(i==j)
I[i][j]=1;
else I[i][j]=0;

}
while(n)
{
if(n%2)
mul(I,A,dim),n--;
else
mul(A,A,dim),n/=2;

}
show(I,dim);
}

int main()
{
int t;
cin>>t;
while(t--)
{
int m,n;
cin>>m>>n;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
cin>>mat[i][j];

}
power(mat,m,n);


}
return 0;

}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you possibly implement the unit test for this algorithm and put it in the corresponding C++ unit test folder?