Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 0.37 KB | None | 0 0
  1. (define (square x) (* x x))
  2.  
  3. (define (divides? a b)
  4.   (= (remainder b a) 0))
  5.  
  6. (define (smallest-divisor n)
  7.   (find-divisor n 2))
  8.  
  9. (define (find-divisor n test-divisor)
  10.   (cond ((> (square test-divisor) n) n)
  11.         ((divides? test-divisor n) test-divisor)
  12.         (else (find-divisor n (+ test-divisor 1)))))
  13.  
  14.  
  15. (define (prime? n)
  16.   (= n (smallest-divisor n)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement