Skip to content

Commit 90b396e

Browse files
committed
Ported the modular series into c++.
1 parent b8aa90a commit 90b396e

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

cpp_scripts/mathematical_algorithms/binary_exponentiation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ long long modulo_binpow(long long a, long long b, long long m)
2020
long long res = 1;
2121
while (b > 0)
2222
{
23-
if (b & 1)
24-
res = res * a % m;
23+
if (b & 1) res = res * a % m;
2524
a = a * a % m;
2625
b >>= 1;
2726
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
int modExp(int x, int y, int p)
2+
{
3+
int res = 1;
4+
int x = x%p;
5+
if(x == 0) return 0;
6+
while(y > 0)
7+
if((y&1) == 1)
8+
{
9+
res = (res*x) % p;
10+
y >>= 1;
11+
x = (x*x) % p;
12+
}
13+
return res;
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
int modFact(int n, int p)
2+
{
3+
if(n >= p) return 0;
4+
int result = 1;
5+
for(int i = 1;i<n+1;i++)
6+
result = (result*i) % p;
7+
return result;
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
int modInverse(int a, int m)
2+
{
3+
int m0 = m, y = 0, x = 1;
4+
if(m == 1) return 0;
5+
while(a > 1)
6+
int q = a/m, t = m, m = a%m, a = t, t = y, y = x-q*y,x = t;
7+
if(x < 0) x = x + m0;
8+
return x;
9+
}

java_scripts/mathematical_algorithms/fibonacci.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,8 @@ int fib(int n)
99
}
1010
void multiply(int F[][], int M[][])
1111
{
12-
int x = F[0][0]*M[0][0] + F[0][1]*M[1][0];
13-
int y = F[0][0]*M[0][1] + F[0][1]*M[1][1];
14-
int z = F[1][0]*M[0][0] + F[1][1]*M[1][0];
15-
int w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
16-
F[0][0] = x;
17-
F[0][1] = y;
18-
F[1][0] = z;
19-
F[1][1] = w;
12+
int x = F[0][0]*M[0][0] + F[0][1]*M[1][0], y = F[0][0]*M[0][1] + F[0][1]*M[1][1], z = F[1][0]*M[0][0] + F[1][1]*M[1][0], w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
13+
F[0][0] = x, F[0][1] = y, F[1][0] = z, F[1][1] = w;
2014
}
2115
void power(int F[][], int n)
2216
{

0 commit comments

Comments
 (0)