[ create a new paste ] login | about

Link: http://codepad.org/q0xT09ee    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Jan 21:
; sum of four primes

(define (prime? n)
  (if (even? n) (= n 2)
    (let loop ((d 3))
      (if (< n (* d d)) #t
        (if (zero? (modulo n d)) #f
          (loop (+ d 2)))))))

(define (goldbach n)
  (let loop ((i 2) (j (- n 2)))
    (if (and (prime? i) (prime? j))
        (list i j)
        (loop (+ i 1) (- j 1)))))

(define (four-primes n)
  (if (< n 8) "impossible"
    (if (even? n)
        (append (list 2 2) (goldbach (- n 4)))
        (append (list 2 3) (goldbach (- n 5))))))

(display (four-primes 46)) (newline)
(display (four-primes 12343209)) (newline)


Output:
1
2
(2 2 5 37)
(2 3 197 12343007)


Create a new paste based on this one


Comments: