divisors   xtlang


Defined in:  https://github.com/digego/extempore/tree/v0.8.9/examples/contrib/word_count.xtm

Implementation

(bind-func divisors
  "find the prime factors of n"
  (lambda (n)
    (letz ((factors (list))
           (d 2))
      (if (< n 2)
          (begin (set! factors (cons n factors))
                 void)
          (while (< d n)
            (if (= (% n d) 0)
                (begin (set! factors (cons d factors))
                       (set! n (/ n d)))
                (if (= d 2)
                    (set! d 3)
                    (set! d (+ d 2))))))
      factors)))


Back to Index

Similar Entries