classSolution {
public:longlong lcm(int a, int b) {
if (a ==0|| b ==0) return0;
longlong aa = std::llabs((longlong)a);
longlong bb = std::llabs((longlong)b);
longlong step = aa > bb ? aa : bb;
longlong ans = step;
while (ans % aa !=0|| ans % bb !=0) ans += step;
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
packagemaintypeSolutionstruct{}
func (sSolution) lcm(a, bint) int {
ifa==0||b==0 { return0 }
aa:=abs(a); ifabs(b) > aa { aa = abs(b) }
ans:=aaforans%a!=0||ans%b!=0 { ans+=aa }
returnans}
funcabs(xint) int { ifx < 0 { return-x }; returnx }
1
2
3
4
5
6
7
8
9
classSolution {
publicintlcm(int a, int b) {
if (a == 0 || b == 0) return 0;
int step = Math.abs(a) > Math.abs(b) ? Math.abs(a) : Math.abs(b);
int ans = step;
while (ans % a != 0 || ans % b != 0) ans += step;
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
classSolution {
funlcm(a: Int, b: Int): Int {
if (a ==0|| b ==0) return0var step = kotlin.math.abs(a)
if (kotlin.math.abs(b) > step) step = kotlin.math.abs(b)
var ans = step
while (ans % a !=0|| ans % b !=0) ans += step
return ans
}
}
1
2
3
4
5
6
7
8
classSolution:
deflcm(self, a: int, b: int) -> int:
if a ==0or b ==0: return0 step = max(abs(a), abs(b))
ans = step
while ans % a !=0or ans % b !=0:
ans += step
return ans
1
2
3
4
5
6
7
8
9
10
pubstructSolution;
impl Solution {
pubfnlcm(&self, a: i64, b: i64) -> i64 {
if a ==0|| b ==0 { return0 }
letmut step = a.abs(); if b.abs() > step { step = b.abs() }
letmut ans = step;
while ans % a !=0|| ans % b !=0 { ans += step; }
ans
}
}
Factor both numbers into primes and take the union of primes with the maximum exponent seen for each prime. Multiplying those primes to their chosen exponents yields the LCM.
#include<map>#include<cmath>#include<cstdlib>classSolution {
public:longlong lcm(int a, int b) {
if (a ==0|| b ==0) return0;
auto A = factor(std::llabs((longlong)a));
auto B = factor(std::llabs((longlong)b));
longlong res =1;
for (auto&kv : A) {
longlong p = kv.first;
int e = kv.second;
int eb = B.count(p) ? B[p] :0;
for (int i =0; i < std::max(e, eb); ++i) res *= p;
}
for (auto&kv : B) if (!A.count(kv.first)) for (int i =0; i < kv.second; ++i) res *= kv.first;
return res;
}
private: std::map<longlong,int> factor(longlong n) {
std::map<longlong,int> mp;
for (longlong p =2; p * p <= n; ++p) {
while (n % p ==0) { mp[p]++; n /= p; }
}
if (n >1) mp[n]++;
return mp;
}
};
import java.util.*;
classSolution {
publicintlcm(int a, int b) {
if (a == 0 || b == 0) return 0;
Map<Integer,Integer> A = factor(Math.abs(a));
Map<Integer,Integer> B = factor(Math.abs(b));
long res = 1;
for (Map.Entry<Integer,Integer> e : A.entrySet()) {
int p = e.getKey(); int ea = e.getValue(); int eb = B.getOrDefault(p,0);
for (int i = 0; i < Math.max(ea, eb); ++i) res *= p;
}
for (Map.Entry<Integer,Integer> e : B.entrySet()) if (!A.containsKey(e.getKey())) for (int i = 0; i < e.getValue(); ++i) res *= e.getKey();
return (int)res;
}
private Map<Integer,Integer>factor(int n) {
Map<Integer,Integer> mp =new HashMap<>();
for (int p = 2; (long)p * p <= n; ++p) {
while (n % p == 0) { mp.put(p, mp.getOrDefault(p,0)+1); n /= p; }
}
if (n > 1) mp.put(n, mp.getOrDefault(n,0)+1);
return mp;
}
}
classSolution {
funlcm(a: Int, b: Int): Int {
if (a ==0|| b ==0) return0val A = factor(kotlin.math.abs(a))
val B = factor(kotlin.math.abs(b))
var res = 1for ((p, ea) in A) {
val eb = B.getOrDefault(p, 0)
repeat(maxOf(ea, eb)) { res *= p }
}
for ((p, eb) in B) if (!A.containsKey(p)) repeat(eb) { res *= p }
return res
}
privatefunfactor(nOrig: Int): MutableMap<Int, Int> {
var n = nOrig
val mp = mutableMapOf<Int, Int>()
var p = 2while (p * p <= n) {
while (n % p ==0) { mp[p] = mp.getOrDefault(p,0) + 1; n /= p }
p++ }
if (n > 1) mp[n] = mp.getOrDefault(n,0) + 1return mp
}
}
classSolution:
deflcm(self, a: int, b: int) -> int:
if a ==0or b ==0:
return0 A = self._factor(abs(a))
B = self._factor(abs(b))
res =1for p, ea in A.items():
eb = B.get(p, 0)
for _ in range(max(ea, eb)):
res *= p
for p, eb in B.items():
if p notin A:
for _ in range(eb): res *= p
return res
def_factor(self, n: int) -> dict:
m = {}
p =2while p * p <= n:
while n % p ==0:
m[p] = m.get(p, 0) +1 n //= p
p +=1if n >1:
m[n] = m.get(n, 0) +1return m
use std::collections::HashMap;
pubstructSolution;
impl Solution {
pubfnlcm(&self, a: i64, b: i64) -> i64 {
if a ==0|| b ==0 { return0 }
let A = self.factor(a.abs());
let B = self.factor(b.abs());
letmut res =1i64;
for (p, ea) in A.iter() {
let eb =*B.get(p).unwrap_or(&0);
for _ in0..std::cmp::max(*ea, eb) { res *=*p; }
}
for (p, eb) in B.iter() { if!A.contains_key(p) { for _ in0..*eb { res *=*p } } }
res
}
fnfactor(&self, mut n: i64) -> HashMap<i64,i32> {
letmut mp = HashMap::new();
letmut p =2;
while p * p <= n {
while n % p ==0 { *mp.entry(p).or_insert(0) +=1; n /= p; }
p +=1;
}
if n >1 { *mp.entry(n).or_insert(0) +=1; }
mp
}
}
classSolution {
public:longlong lcm(int a, int b) {
if (a ==0|| b ==0) return0;
longlong g = gcd(a, b);
return std::llabs((a / g) * (longlong)b);
}
private:longlong gcd(longlong a, longlong b) {
while (b) { longlong t = a % b; a = b; b = t; }
return a >=0? a : -a;
}
};
classSolution {
publicintlcm(int a, int b) {
if (a == 0 || b == 0) return 0;
int g = gcd(a, b);
return Math.abs((a / g) * b);
}
privateintgcd(int a, int b) {
while (b != 0) { int t = a % b; a = b; b = t; }
return Math.abs(a);
}
}
classSolution {
funlcm(a: Int, b: Int): Int {
if (a ==0|| b ==0) return0val g = gcd(a, b)
return kotlin.math.abs((a / g) * b)
}
privatefungcd(aOrig: Int, bOrig: Int): Int {
var a = aOrig
var b = bOrig
while (b !=0) {
val t = a % b; a = b; b = t
}
return kotlin.math.abs(a)
}
}
classSolution:
deflcm(self, a: int, b: int) -> int:
if a ==0or b ==0:
return0 g = self._gcd(a, b)
return abs((a // g) * b)
def_gcd(self, a: int, b: int) -> int:
a, b = abs(a), abs(b)
while b:
a, b = b, a % b
return a
pubstructSolution;
impl Solution {
pubfnlcm(&self, a: i64, b: i64) -> i64 {
if a ==0|| b ==0 { return0 }
let g = self.gcd(a, b);
(a / g * b).abs()
}
fngcd(&self, mut a: i64, mut b: i64) -> i64 {
a = a.abs(); b = b.abs();
while b !=0 { let t = a % b; a = b; b = t; }
a.abs()
}
}