dimanche 20 août 2017

Cannot identify prime number in swift

For some reason, it always gave me the wrong result. It's always isItPrime = true no matter what number was assigned to the "number" variable.

This is my code:

let number = 6
var i = 1
var isItPrime: Bool?
while i < number {
    if number % i == 0 {
        isItPrime = false
    } else {
        isItPrime = true
    }
    i += 1
}
print(isItPrime)

Can somebody explain to me what's wrong with my code and why the isItPrime bool outputs always "true" ?

Aucun commentaire:

Enregistrer un commentaire