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

conceptual algorithms #163

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
22 changes: 22 additions & 0 deletions Fast_Exponentiation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;

ll fastExpo(int x,int n)
{
if (n==0) return 1;
if (n & 1)
return fastExpo(x,n-1) * x;
int y = fastExpo(x,n/2);
return y*y;
}

int main()
{
int base,power;
cin>>base>>power;
cout<<fastExpo(base,power)<<endl;

return 0;
}
31 changes: 31 additions & 0 deletions Modular Exponentation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
using namespace std ;

int power(int x, unsigned int y, int p)
{
int res = 1; // Initialize result

x = x % p; // Update x if it is more than or
// equal to p

while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;

// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}

int main()
{
int x = 2;
int y = 5;
int p = 13;
cout << "Power is " << power(x, y, p);
return 0;
}
76 changes: 76 additions & 0 deletions Segmented Sieve.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <bits/stdc++.h>
using namespace std;


void simpleSieve(int limit, vector<int> &prime)
{
bool mark[limit+1];
memset(mark, true, sizeof(mark));

for (int p=2; p*p<limit; p++)
{
// If p is not changed, then it is a prime
if (mark[p] == true)
{
// Update all multiples of p
for (int i=p*2; i<limit; i+=p)
mark[i] = false;
}
}

// Print all prime numbers and store them in prime
for (int p=2; p<limit; p++)
{
if (mark[p] == true)
{
prime.push_back(p);
cout << p << " ";
}
}
}


void segmentedSieve(int n)
{
int limit = floor(sqrt(n))+1;
vector<int> prime;
simpleSieve(limit, prime);

int low = limit;
int high = 2*limit;

while (low < n)
{
bool mark[limit+1];
memset(mark, true, sizeof(mark));

for (int i = 0; i < prime.size(); i++)
{
int loLim = floor(low/prime[i]) * prime[i];
if (loLim < low)
loLim += prime[i];

for (int j=loLim; j<high; j+=prime[i])
mark[j-low] = false;
}

// Numbers which are not marked as false are prime
for (int i = low; i<high; i++)
if (mark[i - low] == true)
cout << i << " ";

// Update low and high for next segment
low = low + limit;
high = high + limit;
if (high >= n) high = n;
}
}

// Driver program to test above function
int main()
{
int n = 100;
cout << "Primes smaller than " << n << ":n";
segmentedSieve(n);
return 0;
}
47 changes: 47 additions & 0 deletions linearRecurssion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//Template for solving Linear Recurence Relations in LogN time by using fast exponentiation
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned long long int ulli;
#define flash ios_base::sync_with_stdio(false); cin.tie(NULL);
#define mat(x, y, name) vector< vector<ll> > name (x, vector<ll>(y));
#define printMat(name) for (int i = 0; i < name.size(); i++) {for (int j = 0; j < res[i].size(); j++) cout << res[i][j] << " "; cout << endl;}

#define MOD 98765431

vector< vector<ll> > matMul(vector< vector<ll> > A, vector< vector<ll> > B)
{
vector< vector<ll> > C(A.size(), vector<ll>(B[0].size()));
for (int i = 0; i < A.size(); i++)
{
for (int j = 0; j < B[0].size(); j++)
{
C[i][j] = 0;
for (int k = 0; k < B.size(); k++)
C[i][j] = (C[i][j] + ((A[i][k] * B[k][j]) % MOD)) % MOD;
}
}
return C;
}

vector< vector<ll> > matPow(vector< vector<ll> > A, int p)
{
if (p == 1)
return A;
if (p&1)
return matMul(A, matPow(A, p-1));
else
{
vector< vector<ll> > C = matPow(A, p/2);
return matMul(C, C);
}
}

int main()
{
flash;

return 0;
}