Skip to content

Instantly share code, notes, and snippets.

@cametan001
Created July 20, 2010 15:34
Show Gist options
  • Select an option

  • Save cametan001/483118 to your computer and use it in GitHub Desktop.

Select an option

Save cametan001/483118 to your computer and use it in GitHub Desktop.
;; P41 (**) 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
(require srfi/1)
(require "p40.ss")
(define (goldbach-list m n . k)
(let ((m (if (< m 3) 3 m)))
(let ((lst (map goldbach
(iota (ceiling (/ (- n m) 2))
(+ (if (even? m)
0
1) m)
2))))
(for-each (lambda (x)
(for-each display
`(,(apply + x) " = " ,(car x) " + " ,(cadr x) "\n")))
(if (null? k)
lst
(remove (lambda (x)
(< (car x) (car k)))
lst))))))
;; 実行例
;; > (goldbach-list 2 3000 50)
;; 992 = 73 + 919
;; 1382 = 61 + 1321
;; 1856 = 67 + 1789
;; 1928 = 61 + 1867
;; 2078 = 61 + 2017
;; 2438 = 61 + 2377
;; 2512 = 53 + 2459
;; 2530 = 53 + 2477
;; 2618 = 61 + 2557
;; 2642 = 103 + 2539
;; >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment