Swift - Write program to output the list of prime numbers from 2 to 1000

Requirements

Write program to output the list of prime numbers from 2 to 1000

Hint

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Write a function in Swift to output the list of prime numbers from 2 to 1000:

2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
...

Demo

func isPrime(num: Int) -> Bool {
    var prime = true
    var factor = pow(Double(num), 0.5)
    for var i = 2; i <= Int(factor); i++ {
        if (num % i) == 0 {
            prime = false//from  w  w w. j  av  a 2  s .  co  m
        }
    }
    return prime
}

for i in 2...1000 {
    if isPrime(i) {
        print("\(i) is prime")
    }
}

Related Exercise