Skip to content

Instantly share code, notes, and snippets.

@ihercowitz
Created January 9, 2012 17:32
Show Gist options
  • Save ihercowitz/1584031 to your computer and use it in GitHub Desktop.
Save ihercowitz/1584031 to your computer and use it in GitHub Desktop.
Demonstrates the Goldbach's conjecture - Every even integer greater than 2 can be expressed as the sum of two primes
;*******************************************************************************************************************
;Demonstrates the Goldbach's conjecture - Every even integer greater than 2 can be expressed as the sum of two primes
;
;Author: Igor Hercowitz
;
;usage: clisp goldbach.lisp <number>
;output: the sum of the primes list
;
;Ex:
;> clisp goldbach.lisp 100
;97 + 3 95 + 5 91 + 9 89 + 11 85 + 15 83 + 17 79 + 21 77 + 23 73 + 27 71 + 29 67 + 33 65 + 35 61 + 39 59 + 41
;55 + 45 53 + 47 49 + 51 43 + 57 37 + 63 31 + 69 25 + 75 19 + 81 13 + 87 7 + 93
;*******************************************************************************************************************
(defun is_prime(n)
(if (= (mod n 2) 0) (return-from is_prime nil))
(setq x (+ (round (sqrt n)) 1))
(loop for i from 3 to x by 2 do
(if (= (mod n i) 0) (return-from is_prime nil)(return-from is_prime n))))
(defun prime (n)
(cond ( (= n 2) nil)
( (= n 3) (append (list 3)))
(t (cond (
(null (is_prime n)) (prime (- n 1)))
(t (append (list n) (prime (- n 1)) ))
))))
(defun goldbach (val n)
(cond ( (null n) nil)
(t
(setq x (car n))
(setq y (- val x))
(setq n (remove x (remove y n)))
(list (format t "~S + ~S " x y))
(goldbach val n)
)
)
)
(setq value (parse-integer (car *args*)))
(setq prime-list (prime value) )
(print (goldbach value prime-list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment