/* Smallest multipl (Rosetta code) in Picat. http://rosettacode.org/wiki/Smallest_multiple """ Task description is taken from Project Euler (https://projecteuler.net/problem=5) 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ main => go. go => foreach(N in [10,20,200,2000]) println(N=smallest_multiple_range2(N)) end, nl. /* CPU time 17.982 seconds. CPU time 17.554 seconds. CPU time 18.026 seconds. */ go2 => println("Testing 10_000:"), N = 10_000, time(_=smallest_multiple_range1(N)), time(_=smallest_multiple_range2(N)), time(_=smallest_multiple_range3(N)), nl. smallest_multiple_range1(N) = A => A = 1, foreach(E in 2..N) A := lcm(A,E) end. smallest_multiple_range2(N) = fold(lcm, 1, 2..N). smallest_multiple_range3(N) = reduce(lcm, 2..N). % lcm/2 lcm(X,Y) = X*Y//gcd(X,Y). % lcm/3 % lcm(X,Y,LCM) => GCD=gcd(X,Y), LCM = X*Y//GCD.