fork download
  1. ; maximum product of two primes less than n
  2.  
  3. (define (primes n)
  4. (let ((sieve (make-vector (+ n 1) #t)))
  5. (let loop ((p 2)(ps (list)))
  6. (cond ((< n p) (reverse ps))
  7. ((vector-ref sieve p)
  8. (do ((i (* p p) (+ i p))) ((< n i))
  9. (vector-set! sieve i #f))
  10. (loop (+ p 1) (cons p ps)))
  11. (else (loop (+ p 1) ps))))))
  12.  
  13. (define (max-prod-primes n)
  14. (let* ((ps (list->vector (primes (quotient n 2))))
  15. (len (vector-length ps)))
  16. (define (p n) (vector-ref ps n))
  17. (let loop ((lo 0) (hi (- len 1)) (max-lo 0)
  18. (max-hi (- len 1)) (max-prod 0))
  19. (cond ((< hi lo) (list (p max-lo) (p max-hi) max-prod))
  20. ((< n (* (p lo) (p hi)))
  21. (loop lo (- hi 1) max-lo max-hi max-prod))
  22. ((< max-prod (* (p lo) (p hi)))
  23. (loop (+ lo 1) hi lo hi (* (p lo) (p hi))))
  24. (else (loop (+ lo 1) hi max-lo max-hi max-prod))))))
  25.  
  26. (display (max-prod-primes 27)) (newline)
  27. (display (max-prod-primes 50)) (newline)
Success #stdin #stdout 0.02s 7276KB
stdin
Standard input is empty
stdout
(2 13 26)
(7 7 49)