Logo

Programming-Idioms

History of Idiom 75 > diff from v13 to v14

Edit summary for version 14 by :

Version 13

2015-09-05, 16:13:58

Version 14

2015-10-29, 14:05:16

Idiom #75 Compute LCM

Compute the least common multiple x of big integers a and b. Use an integer type able to handle huge numbers.

Idiom #75 Compute LCM

Compute the least common multiple x of big integers a and b. Use an integer type able to handle huge numbers.

Code
defmodule BasicMath do
	def gcd(a, 0), do: a
	def gcd(0, b), do: b
	def gcd(a, b), do: gcd(b, rem(a,b))
	
	def lcm(0, 0), do: 0
	def lcm(a, b), do: (a*b)/gcd(a,b)
end
Code
defmodule BasicMath do
	def gcd(a, 0), do: a
	def gcd(0, b), do: b
	def gcd(a, b), do: gcd(b, rem(a,b))
	
	def lcm(0, 0), do: 0
	def lcm(a, b), do: (a*b)/gcd(a,b)
end
Imports
import "math/big"
Imports
import "math/big"
Code
gcd.GCD(nil, nil, a, b)
x.Div(a, gcd).Mul(x, b)
Code
gcd.GCD(nil, nil, a, b)
x.Div(a, gcd).Mul(x, b)
Comments bubble
LCM is not in the standard library, but can be deduced from GCD.

gcd divides a, by definition.

Chaining is permitted and idiomatic.

a, b, gcd, x have pointer type *big.Int.
Comments bubble
LCM is not in the standard library, but can be deduced from GCD.

gcd divides a, by definition.

Chaining is permitted and idiomatic.

a, b, gcd, x have pointer type *big.Int.