[ create a new paste ] login | about

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

programmingpraxis - Scheme, pasted on Jun 7:
; dixon's method with xor-merge

(define (isqrt n)
  (if (not (and (positive? n) (integer? n)))
      (error 'isqrt "must be positive integer")
      (let loop ((x n))
        (let ((y (quotient (+ x (quotient n x)) 2)))
          (if (< y x) (loop y) x)))))

(define sort #f)
(define merge #f)
(let ()
  (define dosort
    (lambda (pred? ls n)
      (if (= n 1)
          (list (car ls))
          (let ((i (quotient n 2)))
            (domerge pred?
                     (dosort pred? ls i)
                     (dosort pred? (list-tail ls i) (- n i)))))))
  (define domerge
    (lambda (pred? l1 l2)
        ((null? l1) l2)
        ((null? l2) l1)
        ((pred? (car l2) (car l1))
         (cons (car l2) (domerge pred? l1 (cdr l2))))
        (else (cons (car l1) (domerge pred? (cdr l1) l2))))))
  (set! sort
    (lambda (pred? l)
      (if (null? l) l (dosort pred? l (length l)))))
  (set! merge
    (lambda (pred? l1 l2)
      (domerge pred? l1 l2))))

; number theory

(define (primes n)
  (let ((sieve (make-vector (+ n 1) #t)) (ps (list)))
    (do ((p 2 (+ p 1))) ((< n p) (reverse ps))
      (when (vector-ref sieve p)
        (set! ps (cons p ps))
        (do ((i (* p p) (+ i p))) ((< n i))
          (vector-set! sieve i #f))))))

(define (jacobi a n) ;;; check for latest version
  (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))))))

; dixon's algorithm

(define (factor-base n f)
  (let loop ((ps (cdr (primes f))) (fb (list 2)))
    (cond ((null? ps) (reverse fb))
          ((positive? (jacobi n (car ps)))
            (loop (cdr ps) (cons (car ps) fb)))
          (else (loop (cdr ps) fb)))))

(define (smooth n fb) ; large prime variant
  (let loop ((n (abs n)) (fb fb)
             (fs (if (negative? n) (list -1) (list))))
    (cond ((null? fb) (list))
          ((< n (* (car fb) (car fb))) (cons n fs))
          ((zero? (modulo n (car fb)))
            (loop (/ n (car fb)) fb (cons (car fb) fs)))
          (else (loop n (cdr fb) fs)))))

(define (dixon n f)
  (let ((fb (factor-base n f)))
    (let loop ((x (+ (isqrt n) 1)) (t nil))
      (if (= n x) (error 'dixon "exhausted")
        (let* ((x2 (modulo (* x x) n))
               (fs (smooth x2 fb)))
          (if (null? fs) (loop (+ x 1) t)
            (let ((f-or-t (rel n fb t fs (list x))))
              (if (integer? f-or-t) f-or-t
                (loop (+ x 1) f-or-t)))))))))

; binary search tree

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

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

(define (insert t k es hs)
  (cond ((nil? t) (tree k es hs nil nil))
        ((< k (valu t)) (tree (valu t) (expo t) (hist t)
                          (insert (lkid t) k es hs) (rkid t)))
        ((< (valu t) k) (tree (valu t) (expo t) (hist t)
                          (lkid t) (insert (rkid t) k es hs)))
        (else (tree k es hs (lkid t) (rkid t)))))

; linear algebra

(define (dedup xs) ; remove pairs of duplicates
  (let loop ((xs xs) (prev 0) (zs (list)))
    (if (null? xs) (reverse zs)
      (if (= (car xs) prev) (loop (cdr xs) 0 (cdr zs))
        (loop (cdr xs) (car xs) (cons (car xs) zs))))))

(define (xor-merge xs ys) ; add two vectors
  (let loop ((xs xs) (ys ys) (zs (list)))
    (cond ((null? xs) (reverse (append (reverse ys) zs)))
          ((null? ys) (reverse (append (reverse xs) zs)))
          ((< (car xs) (car ys))
            (loop xs (cdr ys) (cons (car ys) zs)))
          ((< (car ys) (car xs))
            (loop (cdr xs) ys (cons (car xs) zs)))
          (else (loop (cdr xs) (cdr ys) zs)))))

(define (rel n fb t es hs) ; return factor or new tree
  (let loop ((es (dedup es)) (hs hs))
    (cond ((null? es)
            (let ((f (calc-gcd n fb hs)))
              (if f f t)))
          ((lookup t (car es)) =>
            (lambda (x)
              (loop (xor-merge (expo x) (cdr es))
                    (xor-merge (hist x) hs))))
          (else (insert t (car es) (cdr es) hs)))))

(define (calc-gcd n fb hs) ; return factor or #f
  (define (square x) (modulo (* x x) n))
  (define (root ys)
    (let loop ((ys (sort < ys)) (y 1))
      (if (null? ys) y
        (loop (cddr ys) (* y (car ys))))))
  (let* ((x (apply * hs))
         (y (root (apply append
           (map (lambda (h) (smooth (square h) fb)) hs))))
         (g (gcd (- x y) n)))
    (if (< 1 g n) g #f)))

(display (dixon (* 493827469 284726159) 2000))


Create a new paste based on this one