calculate prime numbers between 1 and 100 in swift

Here's one approach to calculate prime numbers between 1 and 100 in Swift:

main.swift
func isPrime(_ number: Int) -> Bool {
    if number <= 1 {
        return false
    }
    for i in 2..<number {
        if number % i == 0 {
            return false
        }
    }
    return true
}

for num in 1...100 {
    if isPrime(num) {
        print(num)
    }
}
271 chars
18 lines

This code defines a function isPrime that checks whether a given number is prime or not by dividing it by numbers from 2 up to but not including the number itself. It then uses a loop to iterate over all numbers between 1 and 100, and prints out those that are prime. Note that there are more efficient algorithms for calculating primes, but this should work for small ranges of numbers.

gistlibby LogSnag