Skip to content

Commit 96ed97a

Browse files
authored
Merge pull request ephremdeme#112 from dev-abir/master
add gcd and lcm in java, c++, c and golang
2 parents ab1051f + 0260001 commit 96ed97a

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

Mathematics/GCD/LcmGcd.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class LcmGcd {
2+
3+
// recursive function to return gcd of a and b
4+
private static int gcd(int a, int b) {
5+
/*please see: https://en.wikipedia.org/wiki/Euclidean_algorithm*/
6+
if (b == 0) return a;
7+
return gcd(b, a % b);
8+
}
9+
10+
// function to return LCM of two numbers
11+
private static int lcm(int a, int b) {
12+
/*
13+
a * b = lcm(a, b) * gcd (a, b)
14+
lcm(a, b) = (a * b) / gcd(a, b)
15+
*/
16+
return (a / gcd(a, b)) * b;
17+
}
18+
19+
public static void main(String[] args) {
20+
System.out.println("lcm of 10, 35 is: " + lcm(10, 35));
21+
System.out.println("gcd of 10, 35 is: " + gcd(10, 35));
22+
}
23+
}

Mathematics/GCD/lcm_gcd.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
3+
// recursive function to return gcd of a and b
4+
int gcd(int a, int b) {
5+
/*please see: https://en.wikipedia.org/wiki/Euclidean_algorithm*/
6+
if (b == 0) return a;
7+
return gcd(b, a % b);
8+
}
9+
10+
// function to return LCM of two numbers
11+
int lcm(int a, int b) {
12+
/*
13+
a * b = lcm(a, b) * gcd (a, b)
14+
lcm(a, b) = (a * b) / gcd(a, b)
15+
*/
16+
return (a / gcd(a, b)) * b;
17+
}
18+
19+
int main() {
20+
printf("lcm of 10, 35 is: %d\n", lcm(10, 35));
21+
printf("lcm of 10, 35 is: %d\n", gcd(10, 35));
22+
return 0;
23+
}

Mathematics/GCD/lcm_gcd.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
// recursive function to return gcd of a and b
6+
int gcd(int a, int b) {
7+
/*please see: https://en.wikipedia.org/wiki/Euclidean_algorithm*/
8+
if (b == 0) return a;
9+
return gcd(b, a % b);
10+
}
11+
12+
// function to return LCM of two numbers
13+
int lcm(int a, int b) {
14+
/*
15+
a * b = lcm(a, b) * gcd (a, b)
16+
lcm(a, b) = (a * b) / gcd(a, b)
17+
*/
18+
return (a / gcd(a, b)) * b;
19+
}
20+
21+
int main() {
22+
cout << "lcm of 10, 35 is: " << lcm(10, 35) << "\n";
23+
cout << "gcd of 10, 35 is: " << gcd(10, 35) << "\n";
24+
return 0;
25+
}

Mathematics/GCD/lcm_gcd.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// recursive function to return gcd of a and b
6+
func gcd(a int, b int) int {
7+
/*please see: https://en.wikipedia.org/wiki/Euclidean_algorithm*/
8+
if b == 0 {
9+
return a
10+
}
11+
return gcd(b, a%b)
12+
}
13+
14+
// function to return LCM of two numbers
15+
func lcm(a int, b int) int {
16+
/*
17+
a * b = lcm(a, b) * gcd (a, b)
18+
lcm(a, b) = (a * b) / gcd(a, b)
19+
*/
20+
return (a / gcd(a, b)) * b
21+
}
22+
23+
func main() {
24+
fmt.Println("lcm of 10, 35 is: ", lcm(10, 35))
25+
fmt.Println("lcm of 10, 35 is: ", gcd(10, 35))
26+
}
27+

0 commit comments

Comments
 (0)