dimanche 26 avril 2020

Check if a number is prime with NO loop

Check if a number is Prime with no loop. Looking for feedback on this solution vs while/for loop option.

func isPrime(num: Int) -> Bool{
  if num == 1 {return false}

  if (num != 2) && (num % 2) == 0{
      return false
    }else if (num != 3) && (num % 3) == 0 {
      return false
    }else if (num != 5) && (num % 5) == 0{
      return false
    }else if (num != 7) && (num % 7) == 0{
      return false
    }else{
      return true
  }
}

Aucun commentaire:

Enregistrer un commentaire