Skip to content

Commit 00609ee

Browse files
committed
modular exponentiation code added
1 parent 05c99c9 commit 00609ee

File tree

3 files changed

+199
-167
lines changed

3 files changed

+199
-167
lines changed

C/lcs.c

+82-82
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,82 @@
1-
#include<stdio.h>
2-
#include<stdlib.h>
3-
#include<string.h>
4-
void printMat(int** ar,int n,int m)
5-
{
6-
int i = 0,j = 0;
7-
for(i =0 ; i< n ;i++)
8-
{
9-
for(j = 0; j< m; j++)
10-
{
11-
printf("%d\t", ar[i][j]);
12-
}
13-
printf("\n" );
14-
}
15-
}
16-
void LCSPrint(char* X, int** dir, int n, int m)
17-
{
18-
//printf("%d-%d-%d\n",dir[n][m],n,m );
19-
if(dir[n][m]==1)
20-
{
21-
LCSPrint(X,dir,n-1,m-1);
22-
printf("%c",X[m-1] );
23-
}
24-
else if(dir[n][m] == 2)
25-
{
26-
LCSPrint(X, dir, n-1,m);
27-
}
28-
else if(dir[n][m]==3)
29-
{
30-
LCSPrint(X,dir,n,m-1);
31-
}
32-
}
33-
void LCS(char* p1, char* p2, int n1, int n2)
34-
{
35-
int i = 0, j= 0;
36-
n1++;
37-
n2++;//Important
38-
int **ar = (int**)calloc(n1,sizeof(int*));
39-
int **dir = (int**)calloc(n1,sizeof(int*));
40-
for(i = 0; i< n1+1; i++)
41-
{
42-
ar[i] = (int*)calloc(n2,sizeof(int));
43-
dir[i] = (int*)calloc(n2,sizeof(int));
44-
}
45-
for(i = 1; i< n1; i++)
46-
{
47-
for(j = 1;j< n2; j++)
48-
{
49-
if(p1[i-1] == p2[j-1])
50-
{
51-
ar[i][j] = ar[i-1][j-1]+1;
52-
dir[i][j] = 1;
53-
}
54-
else
55-
{
56-
if(ar[i-1][j] >= ar[i][j-1])
57-
{
58-
ar[i][j] = ar[i-1][j];
59-
dir[i][j] = 2;
60-
}
61-
else
62-
{
63-
ar[i][j] = ar[i][j-1];
64-
dir[i][j] = 3;
65-
}
66-
}
67-
}
68-
}
69-
printMat(ar,n1,n2);
70-
printf("=========\n" );
71-
printMat(dir,n1,n2);
72-
LCSPrint(p2,dir,n1-1,n2-1);
73-
}
74-
int main()
75-
{
76-
char X[] = "ABCBDAB";
77-
char Y[] = "BDCABA";
78-
79-
int m = strlen(X);
80-
int n = strlen(Y);
81-
LCS(X,Y,m,n);
82-
}
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<string.h>
4+
void printMat(int** ar,int n,int m)
5+
{
6+
int i = 0,j = 0;
7+
for(i =0 ; i< n ;i++)
8+
{
9+
for(j = 0; j< m; j++)
10+
{
11+
printf("%d\t", ar[i][j]);
12+
}
13+
printf("\n" );
14+
}
15+
}
16+
void LCSPrint(char* X, int** dir, int n, int m)
17+
{
18+
//printf("%d-%d-%d\n",dir[n][m],n,m );
19+
if(dir[n][m]==1)
20+
{
21+
LCSPrint(X,dir,n-1,m-1);
22+
printf("%c",X[m-1] );
23+
}
24+
else if(dir[n][m] == 2)
25+
{
26+
LCSPrint(X, dir, n-1,m);
27+
}
28+
else if(dir[n][m]==3)
29+
{
30+
LCSPrint(X,dir,n,m-1);
31+
}
32+
}
33+
void LCS(char* p1, char* p2, int n1, int n2)
34+
{
35+
int i = 0, j= 0;
36+
n1++;
37+
n2++;//Important
38+
int **ar = (int**)calloc(n1,sizeof(int*));
39+
int **dir = (int**)calloc(n1,sizeof(int*));
40+
for(i = 0; i< n1+1; i++)
41+
{
42+
ar[i] = (int*)calloc(n2,sizeof(int));
43+
dir[i] = (int*)calloc(n2,sizeof(int));
44+
}
45+
for(i = 1; i< n1; i++)
46+
{
47+
for(j = 1;j< n2; j++)
48+
{
49+
if(p1[i-1] == p2[j-1])
50+
{
51+
ar[i][j] = ar[i-1][j-1]+1;
52+
dir[i][j] = 1;
53+
}
54+
else
55+
{
56+
if(ar[i-1][j] >= ar[i][j-1])
57+
{
58+
ar[i][j] = ar[i-1][j];
59+
dir[i][j] = 2;
60+
}
61+
else
62+
{
63+
ar[i][j] = ar[i][j-1];
64+
dir[i][j] = 3;
65+
}
66+
}
67+
}
68+
}
69+
printMat(ar,n1,n2);
70+
printf("=========\n" );
71+
printMat(dir,n1,n2);
72+
LCSPrint(p2,dir,n1-1,n2-1);
73+
}
74+
int main()
75+
{
76+
char X[] = "ABCBDAB";
77+
char Y[] = "BDCABA";
78+
79+
int m = strlen(X);
80+
int n = strlen(Y);
81+
LCS(X,Y,m,n);
82+
}

modular_exponentiation.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
#define mod 1000000007
4+
#define ll long long int
5+
#define pb push_back
6+
#define mp make_pair
7+
#define rep(a,b,c) for(int i = a; i < b; i+=c)
8+
9+
/* Iterative Function to calculate (x^y) in O(log y) */
10+
int power(int x, unsigned int y)
11+
{
12+
int res = 1; // Initialize result
13+
14+
while (y > 0)
15+
{
16+
// If y is odd, multiply x with result
17+
if (y & 1)
18+
res = res*x;
19+
// y must be even now
20+
y = y>>1; // y = y/2
21+
x = x*x; // Change x to x^2
22+
}
23+
return res;
24+
}
25+
26+
int main()
27+
{
28+
int n,m;
29+
cin>>n>>m;
30+
cout<<power(n,m)<<endl;
31+
return 0;
32+
}

ternary search.c

+85-85
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,85 @@
1-
// C program to illustrate
2-
// recursive approach to ternary search
3-
4-
#include <stdio.h>
5-
6-
// Function to perform Ternary Search
7-
int ternarySearch(int l, int r, int key, int ar[])
8-
{
9-
if (r >= l) {
10-
11-
// Find the mid1 and mid2
12-
int mid1 = l + (r - l) / 3;
13-
int mid2 = r - (r - l) / 3;
14-
15-
// Check if key is present at any mid
16-
if (ar[mid1] == key) {
17-
return mid1;
18-
}
19-
if (ar[mid2] == key) {
20-
return mid2;
21-
}
22-
23-
// Since key is not present at mid,
24-
// check in which region it is present
25-
// then repeat the Search operation
26-
// in that region
27-
28-
if (key < ar[mid1]) {
29-
30-
// The key lies in between l and mid1
31-
return ternarySearch(l, mid1 - 1, key, ar);
32-
}
33-
else if (key > ar[mid2]) {
34-
35-
// The key lies in between mid2 and r
36-
return ternarySearch(mid2 + 1, r, key, ar);
37-
}
38-
else {
39-
40-
// The key lies in between mid1 and mid2
41-
return ternarySearch(mid1 + 1, mid2 - 1, key, ar);
42-
}
43-
}
44-
45-
// Key not found
46-
return -1;
47-
}
48-
49-
// Driver code
50-
int main()
51-
{
52-
int l, r, p, key;
53-
54-
// Get the array
55-
// Sort the array if not sorted
56-
int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
57-
58-
// Starting index
59-
l = 0;
60-
61-
// length of array
62-
r = 9;
63-
64-
// Checking for 5
65-
66-
// Key to be searched in the array
67-
key = 5;
68-
69-
// Search the key using ternarySearch
70-
p = ternarySearch(l, r, key, ar);
71-
72-
// Print the result
73-
printf("Index of %d is %d\n", key, p);
74-
75-
// Checking for 50
76-
77-
// Key to be searched in the array
78-
key = 50;
79-
80-
// Search the key using ternarySearch
81-
p = ternarySearch(l, r, key, ar);
82-
83-
// Print the result
84-
printf("Index of %d is %d", key, p);
85-
}
1+
// C program to illustrate
2+
// recursive approach to ternary search
3+
4+
#include <stdio.h>
5+
6+
// Function to perform Ternary Search
7+
int ternarySearch(int l, int r, int key, int ar[])
8+
{
9+
if (r >= l) {
10+
11+
// Find the mid1 and mid2
12+
int mid1 = l + (r - l) / 3;
13+
int mid2 = r - (r - l) / 3;
14+
15+
// Check if key is present at any mid
16+
if (ar[mid1] == key) {
17+
return mid1;
18+
}
19+
if (ar[mid2] == key) {
20+
return mid2;
21+
}
22+
23+
// Since key is not present at mid,
24+
// check in which region it is present
25+
// then repeat the Search operation
26+
// in that region
27+
28+
if (key < ar[mid1]) {
29+
30+
// The key lies in between l and mid1
31+
return ternarySearch(l, mid1 - 1, key, ar);
32+
}
33+
else if (key > ar[mid2]) {
34+
35+
// The key lies in between mid2 and r
36+
return ternarySearch(mid2 + 1, r, key, ar);
37+
}
38+
else {
39+
40+
// The key lies in between mid1 and mid2
41+
return ternarySearch(mid1 + 1, mid2 - 1, key, ar);
42+
}
43+
}
44+
45+
// Key not found
46+
return -1;
47+
}
48+
49+
// Driver code
50+
int main()
51+
{
52+
int l, r, p, key;
53+
54+
// Get the array
55+
// Sort the array if not sorted
56+
int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
57+
58+
// Starting index
59+
l = 0;
60+
61+
// length of array
62+
r = 9;
63+
64+
// Checking for 5
65+
66+
// Key to be searched in the array
67+
key = 5;
68+
69+
// Search the key using ternarySearch
70+
p = ternarySearch(l, r, key, ar);
71+
72+
// Print the result
73+
printf("Index of %d is %d\n", key, p);
74+
75+
// Checking for 50
76+
77+
// Key to be searched in the array
78+
key = 50;
79+
80+
// Search the key using ternarySearch
81+
p = ternarySearch(l, r, key, ar);
82+
83+
// Print the result
84+
printf("Index of %d is %d", key, p);
85+
}

0 commit comments

Comments
 (0)