August 19, 2024

P40 - Goldbach's conjecture

Goldbach's conjecture says that every positive even number greater than 2 is the sum of two prime numbers.

Example: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case. It has beennumericallyconfirmed up to very large numbers (much larger than we can go with our Lisp system). Write a function to find the two prime numbers that sum up to a given even integer.

Example:* (goldbach 28)(5 23)

lisp

(load "p39.lisp")

;;; Determine primes x and y whose sum is N
(defun goldbach (n)
(if (oddp n)
'none
(terms-sum n (primes-up-to n))
)
)

;;; Returns two numbers from LIST whose sum is K, if any.
;;; Note: the list is in ascending order

(defun terms-sum (k list)
(cond
((null list) nil)
((member (- k (car list)) list)
(list (car list) (- k (car list))))
(t (terms-sum k (cdr list)))
)
)
Be first to comment
Leave a reply