[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/0vYONFAK    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on May 10:
; dixon's factorization algorithm

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

(define verbose? #t)
(define answer #f)
(define fb-limit #f)
(define u #f)

(define (isqrt n)
  (if (zero? n) 0 (if (or (negative? n) (not (integer? n)))
      (error 'isqrt "must be non-negative integer")
      (let loop ((x n))
        (let ((y (quotient (+ x (quotient n x)) 2)))
          (if (< y x) (loop y) x))))))

(define (ilog b n)
  (let loop1 ((lo 0) (b^lo 1) (hi 1) (b^hi b))
    (if (< b^hi n) (loop1 hi b^hi (* hi 2) (* b^hi b^hi))
      (let loop2 ((lo lo) (b^lo b^lo) (hi hi) (b^hi b^hi))
        (if (<= (- hi lo) 1) (if (= b^hi n) hi lo)
          (let* ((mid (quotient (+ lo hi) 2))
                 (b^mid (* b^lo (expt b (- mid lo)))))
            (cond ((< n b^mid) (loop2 lo b^lo mid b^mid))
                  ((< b^mid n) (loop2 mid b^mid hi b^hi))
                  (else mid))))))))

(define (make-prime-gen) ; 2, 3, 5, 7, 11, 13, ..., 8411807363
  (define (cycle . xs) (set-cdr! (last-pair xs) xs) xs)
  (define (expm b e m)
    (let ((times (lambda (x y) (modulo (* x y) m))))
      (cond ((zero? e) 1) ((even? e) (expm (times b b) (/ e 2) m))
      (else (times b (expm (times b b) (/ (- e 1) 2) m))))))
  (define (strong-pseudo-prime? n a)
    (let loop ((r 0) (s (- n 1)))
      (if (even? s) (loop (+ r 1) (/ s 2))
        (if (= (expm a s n) 1) #t
          (let loop ((r r) (s s))
            (cond ((zero? r) #f) ((= (expm a s n) (- n 1)) #t)
            (else (loop (- r 1) (* s 2)))))))))
  (define (prime? n) ; guaranteed to 4759123141 by Gerhard Jaeschke
    (if (< n 2) #f ; and 8411807377 by Charles R Greathouse IV at
      (if (even? n) (= n 2) ; http://math.crg4.com/primes.html
        (if (zero? (modulo n 7)) (= n 7)
          (if (zero? (modulo n 61)) (= n 61)
            (and (strong-pseudo-prime? n 2)
                 (strong-pseudo-prime? n 7)
                 (strong-pseudo-prime? n 61)
                 (not (= n 4759123141))))))))
  (let ((wheel (cons 1 (cons 2 (cons 2 (cons 4 (cycle 2 4 2
          4 6 2 6 4 2 4 6 6 2 6 4 2 6 4 6 8 4 2 4 2 4 8 6 4
          6 2 4 6 2 6 6 4 2 4 6 2 6 4 2 4 2 10 2 10))))))
        (curr 2))
    (lambda ()
      (if (< 8411807363 curr)
          (error 'make-prime-gen "out of range")
          (let loop ((next (+ curr (car wheel))))
            (set! wheel (cdr wheel))
            (if (prime? next)
                (let ((p curr)) (set! curr next) p)
                (loop (+ next (car wheel)))))))))

(define (jacobi a n)
  (if (not (and (integer? a) (integer? n) (positive? n) (odd? n)))
      (error 'jacobi "modulus must be positive odd integer")
      (let jacobi ((a a) (n n))
        (cond ((= a 0) 0) ((= a 1) 1)
              ((= a 2) (case (modulo n 8) ((1 7) 1) ((3 5) -1)))
              ((even? a) (* (jacobi 2 n) (jacobi (quotient a 2) n)))
              ((< n a) (jacobi (modulo a n) n))
              ((and (= (modulo a 4) 3) (= (modulo n 4) 3)) (- (jacobi n a)))
              (else (jacobi n a))))))

(define (make-factor-base n lim)
  (define p (make-prime-gen))
  (p) ; discard 2, which is included despite jacobi
  (let loop ((lim lim) (f (p)) (fs '(2)))
    (cond ((zero? lim) (set! fb-limit f) (reverse fs))
          ((positive? (jacobi n f))
            (loop (- lim 1) (p) (cons f fs)))
          (else (loop lim (p) fs)))))

(define (smooth q fb fb2)
  (define (add-fact f fs)
    (if (and (pair? fs) (= (car fs) f)) (cdr fs) (cons f fs)))
  (let loop ((i 0) (q q) (fb fb) (fb2 fb2) (fs '()))
    (cond ((null? fb) (if (< q (car fb2)) (add-fact q fs) #f)) ; exhausted
          ((< q (car fb2)) (add-fact q fs)) ; found large prime, complete
          ((zero? (modulo q (car fb))) ; found factor
            (loop i (/ q (car fb)) fb fb2 (add-fact (car fb) fs)))
          (else (loop (+ i 1) q (cdr fb) (cdr fb2) fs))))) ; no factor

(define (expo-lt? x y) (< x y))
(define (hist-lt? x y) (< (car x) (car y)))

(define (xor-merge lt? xs ys)
  (let loop ((xs xs) (ys ys) (zs '()))
    (cond ((null? xs) (reverse (append (reverse ys) zs)))
          ((null? ys) (reverse (append (reverse xs) zs)))
          ((lt? (car xs) (car ys)) (loop xs (cdr ys) (cons (car ys) zs)))
          ((lt? (car ys) (car xs)) (loop (cdr xs) ys (cons (car xs) zs)))
          (else (loop (cdr xs) (cdr ys) zs)))))

(define (calc-gcd hs n)
  (let loop ((hs hs) (r2n-prod 1) (r-prod-mod-n 1))
    (if (pair? hs)
        (loop (cdr hs) (* (caar hs) r2n-prod)
          (modulo (* (cadar hs) r-prod-mod-n) n))
        (gcd (- r-prod-mod-n (isqrt r2n-prod)) n))))

(define nil '())
(define nil? null?)
(define tree vector)
(define (expo t) (vector-ref t 0))
(define (hist t) (vector-ref t 1))
(define (lkid t) (vector-ref t 2))
(define (rkid t) (vector-ref t 3))

(define (lookup t k)
  (cond ((nil? t) #f)
        ((< k (car (expo t))) (lookup (lkid t) k))
        ((< (car (expo t)) k) (lookup (rkid t) k))
        (else t)))

(define (update t es hs)
  (cond ((nil? t)
          (when (< (car es) fb-limit) (set! u (+ u 1)))
          (tree es hs nil nil))
        ((< (car es) (car (expo t)))
          (tree (expo t) (hist t) (update (lkid t) es hs) (rkid t)))
        ((< (car (expo t)) (car es))
          (tree (expo t) (hist t) (lkid t) (update (rkid t) es hs)))
        (else (error 'update "can't happen"))))

(define (insert n es hs)
  (cond ((null? es) (calc-gcd hs n))
        ((lookup answer (car es)) =>
          (lambda (x)
            (insert n (xor-merge expo-lt? (expo x) es)
                      (xor-merge hist-lt? (hist x) hs))))
        (else (set! answer (update answer es hs)) 0)))

(define (dixon n)
  (define (square x) (* x x))
  (set! answer nil) (set! u 0)
  (let* ((bound (square (quotient (ilog 10 n) 2)))
         (fb (make-factor-base n bound))
         (fb2 (cons 1 (map (lambda (x) (* x x)) fb))))
    (when verbose? (display "Bound = ") (display bound) (newline))
    (let loop ((r (isqrt n)) (t 0) (s 0))
      (when (and verbose? (zero? (modulo t 1000)))
        (display "Found ") (display u)
        (display " smooth relations plus ") (display (- s u))
        (display " large prime relations in ")
        (display t) (display " trials") (display #\return))
      (let* ((r2n (modulo (* r r) n)) (fs (smooth r2n fb fb2)))
        (if (not fs) (loop (+ r 1) (+ t 1) s)
          (let ((d (insert n fs (list (list r2n r)))))
            (if (< 1 d n) (begin (when verbose? (newline)) d)
              (loop (+ r 1) (+ t 1) (+ s 1)))))))))

(set! verbose? #f)

(display (dixon (/ (- (expt 10 11) 1) 9)))


Output:
1
21649


Create a new paste based on this one


Comments: