;; Function to determine whether a positive integer is prime.
;;
;; Issues:
;;   -  a number x is prime if there is no number y<x such that
;;      y divides x an integer number of times (x/y is an integer).
;;
;; General strategy:
;;   1. special cases: if the number is 1 it is not prime.
;;   2. need to try all numbers < x to determine the answer
;;       (according to my strategy). This certainly works, although
;;       is a little overkill. For example, if x is not divisible by 2, it's
;;       not divisbile by 4, so there is really no need to try, etc. 
;;       My strategy is not the most efficient...
;;
;;
;;   1. is easy 
;;   2. need to count down from x-1 to 2, or count up from 2 to x-1,
;;     stopping if we find a number that divides x.
;;
;;     2.1 we need something that can determine whether one number 
;;         divides another. General idea is to make sure that
;;         the product of the integer part of x/y times y is x.
;;         function divisor defined that does this:

;; divisor consumes two numbers and produces a boolean.
;; 
;; (divisor x y) returns true if y divides x evenly.
;;
;;  note: uses scheme floor function to force x/y to be an integer.

(define (divisor? x y)
  (= x (* (floor (/ x y)) y)))

;;
;;    2.2 we now need to try all possible values for y with divisor?
;;       If any of them report true - we return true, otherwise return
;;       false. Below is an implementation that counts down...


;; prime-helper consumes two numbers and returns a boolean.
;; 
;; (prime-helper x y) returns false if there is any integer <= y that
;;   divides x evenly. This function calls itself recursively to try
;;   all possible values (for the divisor) that are greater than 1.
;;   If we reach the base case of 1, there are no divisors...
;;  
;;  You could also design something similar that counts up from 1 to x...
;;
(define (prime-helper x y)
  (cond
    [(= 1 y) true]
    [(divisor? x y) false]
    [else
     (prime-helper x (- y 1))]))

;;
;; isprime? consumes an integer and produces a boolean
;;
;; (isprime? x) is true only when x is a prime number.
;; 
;;  uses (prime-helper x (- x 1)) to determine whether there is
;;   any number < x that divides x evenly.

(define (isprime? x)
   (cond
    [(= 1 x) false]
    [else
     (prime-helper x (- x 1))]))