Skip to content

Commit

Permalink
Added Some number theory ques in cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
deepralhan26 committed Oct 10, 2020
1 parent 7e92036 commit 0fb67e9
Show file tree
Hide file tree
Showing 13 changed files with 510 additions and 0 deletions.
28 changes: 28 additions & 0 deletions binary exponent loop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>
using namespace std;
long long binpow(long long a, long long b)
{
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL
long int base, power = 1, inputpower;
cin >> base >> inputpower;
long long int ans = 1;



return 0;
}

29 changes: 29 additions & 0 deletions binary exponent recursive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
using namespace std;
long int bin_exp(long int base , long int power )
{
if(power==0)
{
return 1;
}
else
{
long int temp =bin_exp(base,power/2);
if(power%2 == 0)
return (temp*temp);
else
return ((temp*temp)*base);
}
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long int power,base;
cin>>base>>power;
cout<<bin_exp(base,power);

return 0;
}
40 changes: 40 additions & 0 deletions et or pfi using seive way.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;
int phi(int n) {
int result = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0)
n /= i;
result -= result / i;
}
}
if (n > 1)
result -= result / n;
return result;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
const long int MAX = 100;
int etf[MAX + 1];
for(int i=0;i<=MAX;i++)
etf[i] = i;
for(int i=2;i*i<=MAX;i++)
{
if(etf[i]== i)
{
for(long int j = i; j<=MAX ; j+=i)
{
etf[j] -= etf[j]/i;
}
}

}
for(int i=1;i<=25;i++)
cout<<" "<<i<<" "<<etf[i]<<" ";

return 0;
}
29 changes: 29 additions & 0 deletions etf or phi in sqrt(n).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
using namespace std;
int phi(int n)
{
int result = n;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
while (n % i == 0)
n /= i;
result -= result / i;
}
}
if (n > 1)
result -= result / n;
return result;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
cout<<phi(n);
return 0;
}
91 changes: 91 additions & 0 deletions euclid extended.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
long long MOD = 1e9 + 7;

long long gcdsimple(long long a,long long b){
if(b==0)
return a;
else
return gcd(b,a%b);
}
long long mod_pow(long long x, long long y, long long p)
{
long long res = 1;

x = x % p;

if (x == 0) return 0;

while (y > 0)
{

if (y & 1)
res = (res * x) % p;


y = y >> 1;
x = (x * x) % p;
}
return res;
}

long long bin_pow(long long a, long long b)
{
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
long long multi(long long a , long long b , long long m)
{
long long res = 0;
a = a%m;
while (b > 0) {
if (b & 1)
res = (res + a)%m;
a = (a%m + a%m)%m;
b >>= 1;
}
return res;

}
int gcdextended(int a, int b, int& x, int& y)
{
if (b == 0) {
x = 1;
y = 0;
return a;
}
int x1, y1;
int d = gcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
int gcd(int a, int b, int& x, int& y)
{
x = 1, y = 0;
int x1 = 0, y1 = 1, a1 = a, b1 = b;
while (b1) {
int q = a1 / b1;
tie(x, x1) = make_tuple(x1, x - q * x1);
tie(y, y1) = make_tuple(y1, y - q * y1);
tie(a1, b1) = make_tuple(b1, a1 - q * b1);
}
return a1;
}
int32_t main()
{
IOS;



return 0;
}
36 changes: 36 additions & 0 deletions euclidproblem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
long long MOD = 1e9 + 7;

long long gcd(long long a , long long b , long long& x , long long& y)
{
if(b==0)
{
x = 1;
y = 0;
return a;
}
long long x1,y1;
long long d = gcd(b,a%b,x1,y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
int32_t main()
{
IOS;
int a,b,x,y,ans;
while(scanf("%lld %lld", &a, &b) == 2)
{
ans = gcd(a,b,x,y);
cout<<x<<" "<<y<<" "<<ans<<"\n";
}




return 0;
}
26 changes: 26 additions & 0 deletions gcd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <bits/stdc++.h>
using namespace std;
int gcd(int a,int b)
{
if(b==0)
{
return a;
}
else
{
return gcd(b,a%b);
}
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout<<gcd(12,18);



return 0;
}

32 changes: 32 additions & 0 deletions modular exponent loop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
using namespace std;
int power(int x, unsigned int y, int p)
{
int res = 1;

x = x % p;

if (x == 0) return 0;

while (y > 0)
{

if (y & 1)
res = (res * x) % p;


y = y >> 1;
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;
}

30 changes: 30 additions & 0 deletions modular exponent recursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <bits/stdc++.h>
using namespace std;
long int bin_exp(long int base , long int power, int m )
{
base = base % m;
if (power == 0)
{
return 1;
}
else
{
long int temp = bin_exp(base, power / 2);
if (power % 2 == 0)
return ((temp % m) * (temp % m)) % m;
else
return ((temp % m) * (temp % m) * base) % m;
}
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long int power, base;
cin >> base >> power;
cout << bin_exp(base, power);

return 0;
}
Loading

0 comments on commit 0fb67e9

Please sign in to comment.