-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathp41.lisp
52 lines (49 loc) · 1.37 KB
/
p41.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
;;;; (**) A list of Goldbach compositions.
;;;;
;;;; 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 + 7
;;;; 12 = 5 + 7
;;;; 14 = 3 + 11
;;;; 16 = 3 + 13
;;;; 18 = 5 + 13
;;;; 20 = 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 + 919
;;;; 1382 = 61 + 1321
;;;; 1856 = 67 + 1789
;;;; 1928 = 61 + 1867
(in-package :99-problems)
(defun goldbach-list (start end &optional (print-limit 0))
(let* ((goldbach-start (cond ((<= start 4) 4)
((evenp start) start)
(t (1+ start))))
(evens (loop for i from goldbach-start upto end by 2 collect i)))
(loop for e in evens
for primes = (goldbach e)
if (< print-limit (first primes)) do (format t "~d = ~{~d~^ + ~}~%" e primes))))
(define-test goldbach-list-test
(assert-prints
"10 = 3 + 7
12 = 5 + 7
14 = 3 + 11
16 = 3 + 13
18 = 5 + 13
20 = 3 + 17"
(goldbach-list 9 20))
(assert-prints
"992 = 73 + 919
1382 = 61 + 1321
1856 = 67 + 1789
1928 = 61 + 1867"
(goldbach-list 1 2000 50)))