Swift Program to Find the Greatest Common Divisor (GCD)
In this tutorial, we will learn how to find the greatest common divisor (GCD) of two numbers in Swift. We will understand what GCD is, write the algorithm to compute it, and implement it step by step with a final Swift program and output.
What is the Greatest Common Divisor (GCD)?
The greatest common divisor (GCD) of two integers is the largest positive integer that divides both numbers without leaving a remainder. For example, the GCD of 54 and 24 is 6 because 6 is the largest number that divides both 54 and 24.
Finding the GCD is a fundamental problem in mathematics and computer science, and it is often used in problems involving fractions, modular arithmetic, and number theory.
Algorithm to Find GCD
One of the most efficient ways to find the GCD is by using the Euclidean algorithm, which is based on the principle that the GCD of two numbers does not change if the larger number is replaced by its difference with the smaller number.
Steps of the algorithm:
- Take two numbers,
a
andb
. - Check if
b
is 0. If yes, the GCD isa
. - If not, compute
a % b
(remainder of dividinga
byb
). - Replace
a
withb
andb
witha % b
. - Repeat until
b
becomes 0.
This process ensures that we efficiently reduce the problem size in each iteration until the GCD is found.
Step-by-Step Implementation in Swift
Let’s translate the algorithm into Swift code step by step.
1. Define the Function
First, define a function gcd
that takes two integers as parameters. Inside the function, implement the steps of the Euclidean algorithm.
func gcd(_ a: Int, _ b: Int) -> Int {
var num1 = a
var num2 = b
while num2 != 0 {
let remainder = num1 % num2
num1 = num2
num2 = remainder
}
return num1
}
Explanation:
func gcd(_ a: Int, _ b: Int)
: Defines a function namedgcd
that accepts two integersa
andb
.var num1 = a
,var num2 = b
: Create mutable copies of the inputs for processing.while num2 != 0
: The loop continues until the second number becomes 0.let remainder = num1 % num2
: Calculate the remainder whennum1
is divided bynum2
.num1 = num2
,num2 = remainder
: Updatenum1
andnum2
for the next iteration.return num1
: When the loop exits,num1
holds the GCD.
2. Test the Function
Now that the function is ready, let’s test it with some example inputs to ensure it works as expected.
// Test cases
let number1 = 54
let number2 = 24
print("The GCD of \(number1) and \(number2) is \(gcd(number1, number2))")
let number3 = 81
let number4 = 153
print("The GCD of \(number3) and \(number4) is \(gcd(number3, number4))")
In this test case, the function computes the GCD of 54
and 24
, as well as 81
and 153
.
Complete Swift Program
Here’s the complete program with both the function and test cases:
import Foundation
// Function to find the greatest common divisor
func gcd(_ a: Int, _ b: Int) -> Int {
var num1 = a
var num2 = b
while num2 != 0 {
let remainder = num1 % num2
num1 = num2
num2 = remainder
}
return num1
}
// Test cases
let number1 = 54
let number2 = 24
print("The GCD of \(number1) and \(number2) is \(gcd(number1, number2))")
let number3 = 81
let number4 = 153
print("The GCD of \(number3) and \(number4) is \(gcd(number3, number4))")
Output
The GCD of 54 and 24 is 6
The GCD of 81 and 153 is 9