factor.scm 472 B

1234567891011121314151617181920
  1. ;; Factorization algorithms
  2. (define-module (euler factor))
  3. (use-modules (euler primes))
  4. (define-public (trial-factor n)
  5. (let lp ([n n]
  6. [primes (erato (inexact->exact (ceiling (sqrt n))))]
  7. [factors '()])
  8. (cond
  9. [(null? primes) (cons n factors)]
  10. [(> (expt (car primes) 2) n) (cons n factors)]
  11. [(zero? (modulo n (car primes)))
  12. (lp (/ n (car primes)) primes (cons (car primes) factors))]
  13. [else
  14. (lp n (cdr primes) factors)])))