find all prime numbers between 1 to 1000 in swift

main.swift
for number in 2...1000 {
    var isPrime = true
    for i in 2..<number {
        if number % i == 0 {
            isPrime = false
            break
        }
    }
    if isPrime {
        print(number)
    }
}
212 chars
13 lines

We loop through all numbers between 2 and 1000. For each number, we check if it is prime, by dividing it by all the numbers between 2 and the number itself (excluding the number itself). If it is divisible by any number, we mark it as not prime and break the loop. If it is not divisible by any number, we mark it as prime and print it.

gistlibby LogSnag