/* Least common multipl (Rosetta code) in Picat. http://rosettacode.org/wiki/Least_common_multiple """ Compute the least common multiple of two integers. Given m and n, the least common multiple is the smallest positive integer that has both m and n as factors. For example, the least common multiple of 12 and 18 is 36, because 12 is a factor (12 × 3 = 36), and 18 is a factor (18 × 2 = 36), and there is no positive integer less than 36 that has both factors. As a special case, if either m or n is zero, then the least common multiple is zero. One way to calculate the least common multiple is to iterate all the multiples of m, until you find one that is also a multiple of n. If you already have gcd for greatest common divisor, then this formula calculates lcm. lcm(m, n) = (m * n) / (gcd(m, n) One can also find lcm by merging the prime decompositions of both m and n. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. go => L = [ [12,18], [-6,14], [35,0], [7,10], [2562047788015215500854906332309589561,6795454494268282920431565661684282819] ], foreach([X,Y] in L) println((X,Y)=lcm(X,Y)) end, println('1..20'=lcm(1..20)), println('1..50'=lcm(1..50)), % println([lcm(I,J): I in 1..10, J in 1..10]), nl. % lcm/2 lcm(X,Y)= abs(X*Y)//gcd(X,Y). % lcm/3 lcm(X,Y,LCM) => LCM = abs(X*Y)//gcd(X,Y). lcm(List) = fold(lcm,1,List). % gcd(List) = fold(gcd,List.first(),List.tail()).