Skip to content

Commit

Permalink
Merge pull request #57 from riddhi-a23/main
Browse files Browse the repository at this point in the history
Added LCM implementation
  • Loading branch information
imsuraj675 authored Dec 24, 2024
2 parents ae67651 + 8f4b237 commit 0ac40fe
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
42 changes: 42 additions & 0 deletions Number_Theory_Functions/LCM_of_a_and_b/LCM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <cstdlib>

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;
}


36 changes: 36 additions & 0 deletions Number_Theory_Functions/LCM_of_a_and_b/LCM.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
27 changes: 27 additions & 0 deletions Number_Theory_Functions/LCM_of_a_and_b/LCM.py
Original file line number Diff line number Diff line change
@@ -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)

2 changes: 1 addition & 1 deletion RoadMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0ac40fe

Please sign in to comment.