Forgotten Player agucova - 1 year ago 288

Swift Question

I'm trying to make an app on Xcode (*Swift*) that checks whether a number is prime or not.

Can someone help me with some *pseudocode* to check the number?

I need a simple answer, not incredibly advanced algorithms. I'm new to programming.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

A simple method is to check all factors up to the square-root of the number. This is NOT the most efficient method, but is sufficient for most numbers you will likely be inputting in an iPhone app. (see https://en.wikipedia.org/wiki/Primality_test for more complex algorithms).

```
function is_prime(n: int)
if n <= 1
return false
if n <= 3
return true
i = 2
while i*i <= n
if n % i == 0
return false
i = i + 1
return true
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**