diff --git a/Number_Theory_Functions/LCM_of_a_and_b/LCM.cpp b/Number_Theory_Functions/LCM_of_a_and_b/LCM.cpp new file mode 100644 index 0000000..f8397e5 --- /dev/null +++ b/Number_Theory_Functions/LCM_of_a_and_b/LCM.cpp @@ -0,0 +1,42 @@ +#include +#include + +class LCM { +public: + static int gcd(int a, int b) { + if (a == 0 && b == 0) + return 0; + if (a == 0) + return b; + if (b == 0) + return a; + + if (a < 0) + a = -a; + if (b < 0) + b = -b; + + while (b != 0) { + int temp = b; + b = a % b; + a = temp; + } + return a; + } + + static int lcm(int a, int b) { + return (a * b) / gcd(a, b); + } +}; + +int main() { + int a = 111; + int b = 37; + + int ans = LCM::lcm(a, b); + std::cout << ans << std::endl; + + return 0; +} + + diff --git a/Number_Theory_Functions/LCM_of_a_and_b/LCM.java b/Number_Theory_Functions/LCM_of_a_and_b/LCM.java new file mode 100644 index 0000000..bcc6323 --- /dev/null +++ b/Number_Theory_Functions/LCM_of_a_and_b/LCM.java @@ -0,0 +1,36 @@ +import java.util.Scanner; + +public class LCM { + public static int gcd(int a, int b) { + if (a==0 && b==0) + return 0; + if (a==0) + return b; + if (b==0) + return a; + + if (a<0) + a = -a; + if (b<0) + b = -b; + + while (b != 0) { + int temp = b; + b = a % b; + a = temp; + } + return a; + } + + public static int lcm(int a, int b){ + return (a * b)/gcd(a,b); + } + + public static void main(String[] args) { + int a = 111; + int b = 37; + + int ans = lcm(a, b); + System.out.println(ans); + } +} diff --git a/Number_Theory_Functions/LCM_of_a_and_b/LCM.py b/Number_Theory_Functions/LCM_of_a_and_b/LCM.py new file mode 100644 index 0000000..ef450d8 --- /dev/null +++ b/Number_Theory_Functions/LCM_of_a_and_b/LCM.py @@ -0,0 +1,27 @@ +import math + +def gcd(a, b): + if a == 0 and b == 0: + return 0 + if a == 0: + return b + if b == 0: + return a + + a = abs(a) + b = abs(b) + + while b != 0: + a, b = b, a % b + return a + +def lcm(a, b): + return (a * b) // gcd(a, b) + +if __name__ == "__main__": + a = 111 + b = 37 + + ans = lcm(a, b) + print(ans) + diff --git a/RoadMap.md b/RoadMap.md index cd13b7c..bd09af5 100644 --- a/RoadMap.md +++ b/RoadMap.md @@ -32,7 +32,7 @@ These are fundamental algorithms and functions often used in competitive program - [ ] Prime Factorization - [x] [Get all Factors (return a Map)](Number_Theory_Functions/Get_Factors) - [x] [GCD of 'a' and 'b' using Euclidean Algorithm](Number_Theory_Functions/GCD_Using_Euclidean_Algorithm) -- [ ] LCM of 'a' and 'b' +- [x] [LCM of 'a' and 'b'](Number_Theory_Functions/LCM_of_a_and_b) - [x] [Binary Exponentiation (with modulus)](Number_Theory_Functions/Binary_exponentiation_(with_modulus)) - [ ] Get Factorial upto 'n' (with modulus) (return an array) - [ ] Get Inverse Factorial upto 'n' (with modulus) (return an array)