multiplicative inverse of modulo m in scheme

后端 未结 4 1093
自闭症患者
自闭症患者 2021-01-23 16:58

I\'ve written the code for multiplicative inverse of modulo m. It works for most of the initial cases but not for some. The code is below:

(define (inverse x m)
         


        
4条回答
  •  不思量自难忘°
    2021-01-23 17:01

    I think this is the Haskell code on that page translated directly into Scheme:

    (define (inverse p q)
      (cond ((= p 0) #f)
            ((= p 1) 1)
            (else
              (let ((recurse (inverse (mod q p) p)))
                 (and recurse
                      (let ((n (- p recurse)))
                        (div (+ (* n q) 1) p)))))))
    

    It looks like you're trying to convert it from recursive to tail-recursive, which is why things don't match up so well.

提交回复
热议问题