Engaged Employer
Create a function to find if a number is prime.
Anonymous
if there are no other assumptions, you can check for divisible numbers until sqrt(n) func isPrime(n: Int) -> Bool { if n n { return true } if n % i == 0 { return false } } return true } if this function is going to be called multiple times, then you can spend some time pre-computing a list of primes func calculatePrimes(to n: Int) { var pensieve = Array(repeating: 0, count: n) var i = 2 while i < n { if (pensieve[i] == 0) { primeList.append(i) } var j = i while j < n { pensieve[j] = 1 j += i } i += 1 } }
Check out your Company Bowl for anonymous work chats.