Scheme code for Pollard's Rho algorithm -
i trying learn scheme implementing few algorithms.
pollards-rho(n) g(x) = x2 + 1 mod n x ← 2; y ← 2; d ← 1; while d = 1: x ← g(x) y ← g(g(y)) d ← gcd(|x - y|, n) if d = n, return failure. else, return d
i trying implement above algorithm in scheme. appreciated. thanks
an implementation in racket:
;;; algorithm 19.8 pollard's rho method ; input n>=3 neither prime nor perfect power ; output either proper divisor of n or #f (: pollard : natural -> (u natural false)) (define (pollard n) (let ([x0 (random-natural n)]) (do ([xi x0 (remainder (+ (* xi xi) 1) n)] [yi x0 (remainder (+ (sqr (+ (* yi yi) 1)) 1) n)] [i 0 (add1 i)] [g 1 (gcd (- xi yi) n)]) [(or (< 1 g n) (> (sqrt n))) (if (< 1 g n) (cast g natural?) #f)])))