projecteuler_problem5

 15th March 2024 at 12:04pm

What is the smallest number divisible by each of the numbers 1 to 20?

The above is almost a definition of the least common multiple, and so it is almost trivial to setup a recursive call

lcm(20, lcm(19, lcm(18, ... 1)))...)

In python, this is equivalent to

from math import lcm
from functools import reduce
reduce(lambda x, y: lcm(x, y), list(range(1, 21)), 1)

which correctly yields the answer.

In Scheme, however...lcm is built-in, but the rest is not; and of course there's some degree of frantically rummaging through documentation in the Chicken docs.

(import test)
(import (only srfi-196
			  range->list
			  numeric-range))

(define (get-lcm-from-1-to-n n)
  (foldl lcm 1 (range->list (numeric-range 1 (+ n 1)))))
(test "example is correct"
	  2520
	  (get-lcm-from-1-to-n 10))

(define (solve-problem-5)
  (get-lcm-from-1-to-n 20))

(print (solve-problem-5))