Skip to content

Commit

Permalink
number theory problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 authored Jun 4, 2024
1 parent d17fdcb commit ade1477
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
24 changes: 24 additions & 0 deletions part1/coprimeDivisor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<bits/stdc++.h>
using namespace std;

int gcd(int a, int b) {return b==0?a:gcd(b,a%b);}

int solve(int a, int b){

int value = 1, x = 0;
while(value < max(a,b)) {
if(a%value == 0 and gcd(value,b)==1)
x = value;
value++;
}

return x;
}

int main(){

int a,b; cin >> a >> b;
cout << solve(a,b) << endl;

return 0;
}
45 changes: 45 additions & 0 deletions part1/extendendEuclideans.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <bits/stdc++.h>
using namespace std;

/*
find the values of x and y thats
A * x + B * y = gcd(A,B)
where A and B are inputs
*/
int gcd(int a, int b) {
return b==0?a:gcd(b,a%b);
}

pair<int,int> extendedGCD(int a, int b) {

// base case
if(b==0) {
// returning the values of x and y
return {1,0};
}

pair<int,int> result = extendedGCD(b,a%b);
int smallX = result.first;
int smallY = result.second;

int x = smallY;
int y = smallX - floor(a/b)*smallY;

return {x,y};

}


int main(){

int a,b; cin >> a >> b;
// a x + b y = gcd(a,b);
pair<int,int> ans = extendedGCD(a,b);
cout << ans.first << " " << ans.second << endl;

return 0;
}

19 changes: 19 additions & 0 deletions part1/gcd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <bits/stdc++.h>
using namespace std;

int gcd(int a, int b) {

if(b == 0)
return a;

return gcd(b,a%b);

}

int main(){

int a,b; cin >> a >> b;
cout << gcd(a,b) << endl;

return 0;
}

0 comments on commit ade1477

Please sign in to comment.