Tuesday, April 24, 2012

Checking Prime Numbers in Scheme


;Function to check whether a given number is prime

(define (prime? n)
    (test-prime n 2)
)

(define (factor a b)
  (= (modulo a b) 0)
)

(define (test-prime n test-no)
  (if (> test-no (/ n 2))
      #t
      (if (factor n test-no)
   #f
   (test-prime n (+ test-no 1))
      )
  )
)

No comments:

Post a Comment