LCM of Two Numbers
Find the least common multiple of two numbers using LCM = a * b / GCD.
Sample input
4, 6
Sample output
12
Solution
def gcd(a, b):
while b:
a, b = b, a % b
return a
a, b = 4, 6
print(a * b // gcd(a, b))
function gcd(a, b) {
while (b) {
[a, b] = [b, a % b];
}
return a;
}
const a = 4, b = 6;
console.log((a * b) / gcd(a, b));
public class Main {
static int gcd(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
public static void main(String[] args) {
int a = 4, b = 6;
System.out.println(a * b / gcd(a, b));
}
}
fun gcd(a: Int, b: Int): Int {
var x = a
var y = b
while (y != 0) {
val t = y
y = x % y
x = t
}
return x
}
fun main() {
val a = 4
val b = 6
println(a * b / gcd(a, b))
}
func gcd(_ a: Int, _ b: Int) -> Int {
var x = a, y = b
while y != 0 {
let t = y
y = x % y
x = t
}
return x
}
let a = 4, b = 6
print(a * b / gcd(a, b))
int gcd(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
void main() {
int a = 4, b = 6;
print(a * b ~/ gcd(a, b));
}
#include <iostream>
using namespace std;
int gcd(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
int main() {
int a = 4, b = 6;
cout << a * b / gcd(a, b) << endl;
return 0;
}
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
int main() {
int a = 4, b = 6;
printf("%d\n", a * b / gcd(a, b));
return 0;
}