-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime.scm
19 lines (15 loc) · 851 Bytes
/
prime.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
;; Procedure to check if a number is prime by finding the smallest divisor for a number as checking if it is equal to itself
(define (square x) (* x x))
;; Check if a divides b without any remainder
(define (divides? a b)
(= (remainder b a) 0))
;; Gets the smallest divisor for a given number
(define (smallest-divisor num)
(define (find-divisor num test-divisor) ;; recursive block
(cond ((> (square test-divisor) num) num) ;; if test-divisor > num^2 then num is smallest divisor
((divides? test-divisor num) test-divisor) ;; does test-divisor divide num
(else (find-divisor num (+ test-divisor 1))))) ;; try next integer
(find-divisor num 2)) ;; start with 2 as the test-divisor
;; Check is a number is prime i.e smallest divisor is equal to itself
(define (prime? num)
(= num (smallest-divisor num)))