Reputation: 33
I am so close to having this cracked, but I cannot see what I'm missing. is-prime? should return true if the number inputted is a prime number or it should return false if the number is not prime. The function I have created works great apart from it will not return false for the number 1. As part of the spec, I was told to set both numbers 1 and 2 which is what I tried doing but it doesn't seem to be doing anything.
(defn is-prime? [n]
(if (= n 1) false)
(if (= n 2) no-divisors?)
(no-divisors? n)
)
Here is a list of all the inputs and expected outcomes for is-prime? The only one that returns incorrect is(is-prime? 1).
(is-prime? 1)
=> false
(is-prime? 2)
=> true
(is-prime? 3)
=> true
(is-prime? 4)
=> false
(is-prime? 101)
=> true
Upvotes: 0
Views: 128
Reputation: 16045
In clojure the result of a function is its last expression, so in your example (if (= n 1) false)
and (if (= n 2) no-divisors?)
are always lost, because the last expression is (no-divisors? n)
.
Try this:
(defn is-prime? [n]
(cond
(= n 1) false
(= n 2) true
:else (no-divisors? n)))
Upvotes: 1