-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler005.pro
33 lines (28 loc) · 963 Bytes
/
euler005.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
% Problem 5: Smallest multiple
% ----------------------------
% 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 (with no
% remainder) by all of the numbers from 1 to 20?
%
% =============================================================================
%
% This problem becomes absolutely trivial if you remember that the least
% common multiple operation is associative.
%
% Implementation notes:
% - Using 1 as initial value in foldl/4 does not alter the final result.
/** <examples>
?- euler005([1,20],R).
*/
:- use_module(library(lists),[numlist/3]).
:- use_module(library(apply),[foldl/4]).
:- use_module(library(statistics),[time/1]).
test:-
writeln(euler005([1,20],232792560)),
time(euler005([1,20],232792560)).
euler005([L,H],R):-
numlist(L,H,LX),
foldl(lcm,LX,1,R).
lcm(X,Y,Z):-
Z is X*Y//gcd(X,Y).