Project Euler: Problem 10

Challenge:

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

Programming Language:

LISP

Solution:

(defun isprime-p(num)
  (defparameter isprime T)
  (if (= num 2)
      (setq isprime T)
      (if (< 1 num 10)
	  (loop named loop-1 for x from 2 below num
	       do (if (= (mod num x) 0)
		      (progn (setq isprime NIL)
			     (return-from loop-1 NIL))))
	  (if (>= num 10)
	      (loop named loop-2 for x from 2 to (+ (isqrt num) 1)
		 do (if (or (= (mod num x) 0) (= (- (sqrt num) (isqrt num)) 0)) 
			(progn (setq isprime NIL)
			       (return-from loop-2 NIL))))
	      (setq isprime NIL))))
 (print isprime))

(defun gen-primes (how-many)
  (defparameter primes ())
  (loop for x from 1 to how-many
     do (if (isprime-p x)
	    (push x primes)))
  (format t "Sum of all primes below ~a is: ~a" how-many (reduce '+ primes)))

Overview:

Coming soon…