[ create a new paste ] login | about

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

programmingpraxis - Scheme, pasted on Sep 30:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
; wheel factorization

(define (last-pair xs)
  (if (null? (cdr xs)) xs
    (last-pair (cdr xs))))

(define (cycle . xs)
  (set-cdr! (last-pair xs) xs) xs)

(define (factors n)
  (let ((wheel (cons 1 (cons 2 (cons 2 (cycle 4 2 4 2 4 6 2 6))))))
    (let loop ((n n) (f 2) (fs (list)) (wheel wheel))
      (cond ((< n (* f f)) (reverse (cons n fs)))
            ((zero? (modulo n f)) (loop (/ n f) f (cons f fs) wheel))
            (else (loop n (+ f (car wheel)) fs (cdr wheel)))))))

(display (factors 600851475143))


Output:
1
(71 839 1471 6857)


Create a new paste based on this one


Comments: