In this tutorial, we will learn how to check if a number is prime in Swift. We will understand what a prime number is, write an algorithm to determine primality, and implement it step by step with a complete Swift program and output.
What is a Prime Number?
A prime number is a natural number greater than 1 that has no divisors other than 1 and itself. For example, 2
, 3
, 5
, and 7
are prime numbers, while 4
, 6
, and 8
are not because they have divisors other than 1 and themselves.
Prime numbers play a crucial role in mathematics and computer science, particularly in cryptography and number theory.
Algorithm to Check if a Number is Prime
The algorithm to check if a number n
is prime is as follows:
- Check if
n
is less than 2. If yes, it is not a prime number. - Iterate through numbers from
2
to the square root ofn
(inclusive). - For each number in this range, check if it divides
n
without a remainder. - If any number divides
n
, it is not a prime number; otherwise, it is prime.
This approach is efficient because it reduces the number of iterations by only testing divisors up to the square root of the number.
Step-by-Step Implementation in Swift
Let’s convert the algorithm into Swift code step by step.
1. Handle Base Cases
First, write a function that handles base cases for numbers less than 2, as these are not prime numbers:
func isPrime(_ n: Int) -> Bool {
if n < 2 {
return false // Numbers less than 2 are not prime
}
return true
}
2. Check Divisors Up to Square Root
Next, extend the function to iterate through possible divisors up to the square root of the number:
func isPrime(_ n: Int) -> Bool {
if n < 2 {
return false
}
for i in 2...Int(Double(n).squareRoot()) {
if n % i == 0 {
return false // Found a divisor, not prime
}
}
return true // No divisors found, number is prime
}
Explanation:
if n < 2
: Eliminates numbers less than 2 as non-prime.
for i in 2...Int(Double(n).squareRoot())
: Iterates through possible divisors up to the square root of n
.
if n % i == 0
: Checks if i
divides n
without a remainder. If true, n
is not prime.
return true
: If no divisors are found, the number is prime.
3. Test the Function
Let’s test the function with some example inputs:
// Test cases
let number1 = 7
print("\(number1) is prime: \(isPrime(number1))")
let number2 = 10
print("\(number2) is prime: \(isPrime(number2))")
let number3 = 1
print("\(number3) is prime: \(isPrime(number3))")
The function will determine whether each number is prime or not.
Complete Swift Program
Here’s the complete program combining the isPrime
function and test cases:
main.swift
import Foundation
// Function to check if a number is prime
func isPrime(_ n: Int) -> Bool {
if n < 2 {
return false // Numbers less than 2 are not prime
}
for i in 2...Int(Double(n).squareRoot()) {
if n % i == 0 {
return false // Found a divisor, not prime
}
}
return true // No divisors found, number is prime
}
// Test cases
let number1 = 7
print("\(number1) is prime: \(isPrime(number1))")
let number2 = 10
print("\(number2) is prime: \(isPrime(number2))")
let number3 = 1
print("\(number3) is prime: \(isPrime(number3))")
let number4 = 29
print("\(number4) is prime: \(isPrime(number4))")
Output
7 is prime: true
10 is prime: false
1 is prime: false
29 is prime: true
Screenshot from Xcode