# Ch. 7 Functions: Challenge A- It's Prime Time

The challenge asks for you to write code that determines if a number is prime.

I’m having troubles figuring out one line of code from the solutions. It’s down below next to my comment.

Challenge A
func isNumberDivisible(number: Int, by byNumber: Int) → Bool {
return number % byNumber == 0
}

func isPrime(number: Int) → Bool {
if number < 0 {
return false
}

if number <= 3 {
return true
}

let root = Int(sqrt(Double(number))) // WHAT IS THIS LINE DOING?
for i in 2…root {
if isNumberDivisible(number, by: i) {
return false
}
}
return true
}

OK, unwrap it from the inside out!

First, it takes the value of number and makes it a Double (it was an Int when received by this function). Double is the required type to use the sqrt function to get the square root of a value. Computers are stupid, remember? 4.0 is not even remotely close to the same thing as 4 unless you explicitly cast it to be the same.
Next the sqrt function is used to get the square root of the value. The result of sqrt is also a Double, that is, a double-precision floating point number. But you don’t want that, you want an Int so that it can be a value in a loop range - so it is cast to Int, acceptable for a loop range specifier.

1 Like

Thank you very much. I just learned casting Ints to Double in the process as well.