Lucas-Lehmer test for Mersenne numbers (Scheme)

From LiteratePrograms

Jump to: navigation, search
Other implementations: Erlang | Haskell | J | Java | Lisp | Python | Ruby | Scheme

This program is a code dump.
Code dumps are articles with little or no documentation or rearrangement of code. Please help to turn it into a literate program. Also make sure that the source of this code does consent to release it under the MIT or public domain license.


(define lucas-lehmer
  (lambda (p)
    (define loop
      (lambda (s M count)
        (if (> count 0)
            (loop (remainder (- (* s s) 2) M) M (- count 1))
            s)))
    (let* ([s 4]
           [M (sub1 (expt 2 p))]
           [result (loop s M (- p 2))])
      (= result 0))))
Download code
Views