Paste number 60570: simple factor implementation

Paste number 60570: simple factor implementation
Pasted by: foof
5 days, 21 hours ago
#scheme | Context in IRC logs
Paste contents:
Raw Source | XML | Display As
(define (factor n)
  (cond
    ((negative? n)
     (cons -1 (factor (- n)))
)

    ((<= n 2)
     (list n)
)

    (else
     (let lp ((n n) (limit (inexact->exact (ceiling (sqrt n)))) (res (list)))
       (cond
         ((even? n)
          (lp (quotient n 2) (quotient limit 2) (cons 2 res))
)

         ((= n 1)
          (reverse res)
)

         (else
          (let lp ((i 3) (n n) (limit limit) (res res))
            (cond
              ((> i limit)
               (reverse (cons n res))
)

              ((zero? (remainder n i))
               (lp i (quotient n i) (quotient limit i) (cons i res))
)

              (else
               (lp (+ i 2) n limit res)
)
)
)
)
)
)
)
)
)

This paste has no annotations.

Colorize as:
Show Line Numbers

Ads absolutely not by Google

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.