August 19, 2024
P42 - A list of Goldbach compositions (2)
Given a range of integers by its lower and upper limit, print a list of all even numbers and their Goldbach composition.
Example:* (goldbach-list 9 20)10 = 3 + 712 = 5 + 714 = 3 + 1116 = 3 + 1318 = 5 + 1320 = 3 + 17
In most cases, if an even number is written as the sum of two prime numbers, one of them is very small. Very rarely, the primes are both bigger than say 50. Try to find out how many such cases there are in the range 2..3000.
Example (for a print limit of 50):* (goldbach-list 1 2000 50)992 = 73 + 9191382 = 61 + 13211856 = 67 + 17891928 = 61 + 1867
lisp
(load "p40.lisp")
(load "p22.lisp")
;;; Imprime os termos de Goldbach para pares entre K e N
(defun goldbach-list (k n &optional (lower-bound 1))
(mapcar #'(lambda (p)
(format t "~D = ~D + ~D~%"
(+ (first p) (second p)) (first p) (second p)))
(goldbach-between k n lower-bound)
)
'end
)
;;; Determina os termos de Goldbach para pares entre K e N
(defun goldbach-between (k n &optional (lower-bound 1))
(cond
((or (<= n 1) (< n k)) ())
(t (goldbach-between-aux k n lower-bound))
)
)
;;; Funcao auxiliar de goldbach-between
(defun goldbach-between-aux (k n &optional (lower-bound 1))
(remove-if #'(lambda (p) (< (car p) lower-bound))
(remove 'none (mapcar #'goldbach (range k n))))
)