From ade147753ac584603780d7726796ff75afc8bcb3 Mon Sep 17 00:00:00 2001 From: "Paulo H. Lamounier" <53798700+Nanashii76@users.noreply.github.com> Date: Tue, 4 Jun 2024 12:21:04 -0300 Subject: [PATCH] number theory problems --- part1/coprimeDivisor.cpp | 24 +++++++++++++++++++ part1/extendendEuclideans.cpp | 45 +++++++++++++++++++++++++++++++++++ part1/gcd.cpp | 19 +++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 part1/coprimeDivisor.cpp create mode 100644 part1/extendendEuclideans.cpp create mode 100644 part1/gcd.cpp diff --git a/part1/coprimeDivisor.cpp b/part1/coprimeDivisor.cpp new file mode 100644 index 0000000..4d7921e --- /dev/null +++ b/part1/coprimeDivisor.cpp @@ -0,0 +1,24 @@ +#include +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; +} \ No newline at end of file diff --git a/part1/extendendEuclideans.cpp b/part1/extendendEuclideans.cpp new file mode 100644 index 0000000..d1365da --- /dev/null +++ b/part1/extendendEuclideans.cpp @@ -0,0 +1,45 @@ +#include +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 extendedGCD(int a, int b) { + + // base case + if(b==0) { + // returning the values of x and y + return {1,0}; + } + + pair 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 ans = extendedGCD(a,b); + cout << ans.first << " " << ans.second << endl; + + return 0; +} + diff --git a/part1/gcd.cpp b/part1/gcd.cpp new file mode 100644 index 0000000..e230e4d --- /dev/null +++ b/part1/gcd.cpp @@ -0,0 +1,19 @@ +#include +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; +} \ No newline at end of file