Project Euler: Problem 5

Challenge:

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?

Programming Language:

LISP

Solution:

(defun smul (lim)
  (defparameter nlim (apply '* (getprimes lim)))
  (defparameter num nlim)
  (loop while (/= 0 (apply '+ (mapcar (lambda (x) (mod num x))
				      '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20))))
     do (setf num (+ num nlim))
       (format t "num is ~a~%: " num)))

(defun isprime-p(n)
  (defparameter isprime NIL)
  (if (and (oddp n) (> n 2))
      (loop for x from 2 below n
	 do (if (eql 0 (mod n x))
		(progn (setq isprime NIL)
		       (return NIL))
		(setq isprime T))))
  (if (eq n 2)
      (setq isprime T))
  (print isprime))

(defun getprimes(lim)
  (defparameter primes NIL)
  (loop for x from 1 to lim
     do (if (eql T (isprime-p x))
	    (push x primes)))
  (print primes))

Overview:

This solution first generates all primes from 1 – “lim” or 20 in this example. Once primes are generated, they are multiplied together and the product is used to generate multiples of that number. These multiples are then tested in smul().