In this tutorial, we will learn how to check if a number is a perfect number in Swift. We will understand the concept of a perfect number, write an algorithm to determine it, and implement the solution step by step with a complete Swift program and output.
What is a Perfect Number?
A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). For example:
6
is a perfect number because its divisors (excluding itself) are1, 2, 3
, and their sum is1 + 2 + 3 = 6
.28
is a perfect number because its divisors (excluding itself) are1, 2, 4, 7, 14
, and their sum is1 + 2 + 4 + 7 + 14 = 28
.12
is not a perfect number because its divisors (excluding itself) are1, 2, 3, 4, 6
, and their sum is1 + 2 + 3 + 4 + 6 = 16
.
We will now write an algorithm to determine if a number is a perfect number.
Algorithm to Check Perfect Number
To check if a number is a perfect number, follow these steps:
- Take a positive integer as input.
- Initialize a variable
sum
to 0 to store the sum of divisors. - Iterate from
1
tonumber / 2
(inclusive) to find all divisors:- Check if the current number divides the input number without a remainder.
- If yes, add the current number to
sum
.
- After the loop, compare
sum
with the original number: - If they are equal, the number is a perfect number; otherwise, it is not.
This algorithm ensures that only proper divisors are considered, and the calculation is efficient by limiting the iteration to half of the number.
Step-by-Step Implementation in Swift
Let’s implement the algorithm in Swift step by step.
1. Writing function to check Perfect Number
Create a function to initialize the sum
variable and iterate through potential divisors:
func isPerfectNumber(_ number: Int) -> Bool {
if number <= 1 {
return false // Perfect numbers must be greater than 1
}
var sum = 0 // Initialize sum of divisors
for i in 1...(number / 2) {
if number % i == 0 {
sum += i // Add the divisor to the sum
}
}
return sum == number // Check if the sum equals the original number
}
Explanation:
if number <= 1
: Ensures the number is greater than 1 because perfect numbers are positive integers.
for i in 1...(number / 2)
: Iterates through all potential divisors up to half the number.
if number % i == 0
: Checks if i
is a divisor of the number.
sum += i
: Adds the divisor to the running total.
return sum == number
: Compares the sum of divisors with the original number to determine if it is a perfect number.
2. Test the Function
Let’s test the function with some example inputs:
// Test cases
print("6 is a perfect number: \(isPerfectNumber(6))") // true
print("28 is a perfect number: \(isPerfectNumber(28))") // true
print("12 is a perfect number: \(isPerfectNumber(12))") // false
print("1 is a perfect number: \(isPerfectNumber(1))") // false
The function correctly identifies whether the numbers are perfect numbers or not.
Complete Swift Program
Here’s the complete Swift program to find the sum of the digits of a number:
import Foundation
// Function to check if a number is a perfect number
func isPerfectNumber(_ number: Int) -> Bool {
if number <= 1 {
return false // Perfect numbers must be greater than 1
}
var sum = 0 // Initialize sum of divisors
for i in 1...(number / 2) {
if number % i == 0 {
sum += i // Add the divisor to the sum
}
}
return sum == number // Check if the sum equals the original number
}
// Test cases
print("6 is a perfect number: \(isPerfectNumber(6))") // true
print("28 is a perfect number: \(isPerfectNumber(28))") // true
print("12 is a perfect number: \(isPerfectNumber(12))") // false
print("1 is a perfect number: \(isPerfectNumber(1))") // false
Output
6 is a perfect number: true
28 is a perfect number: true
12 is a perfect number: false
1 is a perfect number: false
Screenshot from Xcode