[ create a new paste ] login | about

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

programmingpraxis - Scheme, pasted on Apr 30:
; legendre's symbol

(define (jacobi a m)
  (if (not (integer? a)) (error 'jacobi "must be integer")
    (if (not (and (integer? m) (positive? m) (odd? m)))
        (error 'jacobi "modulus must be odd positive integer")
        (let loop1 ((a (modulo a m)) (m m) (t 1))
          (if (zero? a) (if (= m 1) t 0)
            (let ((z (if (member (modulo m 8) (list 3 5)) -1 1)))
              (let loop2 ((a a) (t t))
                (if (even? a) (loop2 (/ a 2) (* t z))
                  (loop1 (modulo m a) a
                         (if (and (= (modulo a 4) 3)
                                  (= (modulo m 4) 3))
                             (- t) t))))))))))

(define (legendre a m)
  (if (prime? m) (jacobi a m)
    (error 'legendre "modulus must be prime")))

(display (jacobi -1 7)) (newline)
(display (jacobi 3 3)) (newline)
(display (jacobi 127 703)) (newline)


Output:
1
2
3
-1
0
-1


Create a new paste based on this one


Comments: