Swift program to find the GCD of two given numbers using recursion


This tutorial will discuss how to write swift program to find the GCD of two given numbers using recursion.

GCD(Greatest Common Factor) of two numbers is known as the greater positive number which is the common factor of both the given two positive numbers. GCD is also known as HCF(Highest Common Factor). GCD of two numbers cannot be negative or zero and the minimum GCD value of two numbers is always 1. For example, suppose we have two numbers: 16 and 24

So the factor of 16 = 2x2x2x2

The factor of 24 = 2x2x2x3

So, GCD(16, 24) = 2x2x2x2 = 8

Below is a demonstration of the same −

Input

Suppose our given input is −

Num1 = 16
Num2 = 24

Output

The desired output would be −

GCD = 8

Euklid’s Algorithm

To calculate GCD of two number, we first calculate their factors and then then take the greatest common number they have to calculate GCD. This method work perfectly fine for small number but what if the numbers are large, then this method gets difficult. So, to find the GCD of small or large numbers we use Euklid’s Algorithm. In this algorithm, we divide two numbers till the remainder become 0.

GCD(x, y) = GCD(y, x%y)

Here, x%y is used to find the remainder.

To calculate the GCD of two numbers we can use recursive approach. Recursive approach is an approach in which a function call itself to complete the specified task.

Algorithm

Following is the algorithm −

Step 1 − Create a recursive function.

Step 2 − Declare a variable named “output” to store remainder.

let output: Int = n1 % n2

Step 3 − Check if output != 0. If condition is true then call the findRecursiveGCD() function recursively. Otherwise return n2 because the reminder is zero which means we find the GCD of two number.

Step 4 − Calling the function and store the result into a variable.

Step 5 − Print the output.

Example 1

The following program shows how to calculate the GCD of two given numbers using recursion.

import Swift
import Foundation
// Recursive function to find gcd of two numbers
func findRecursiveGCD(n1: Int, n2: Int) -> Int {
   let output: Int = n1 % n2
   // Base condition
   if output != 0{
      // Calling function itself
      return findRecursiveGCD(n1: n2, n2: output)
   } else{
      return n2
   }
}
// Calling Function
var result = findRecursiveGCD(n1:27, n2:21)
print("GCD of 27 and 21 is ", result)

Output

GCD of 27 and 21 is  3

Here in the above code, we create a recursive function named findRecursiveGCD to calculate the GCD of two numbers. It uses Euklid’s algorithm. Here in this function, we find the remainder of two numbers by calling the function recursively. So the working of the findRecursiveGCD() −

findRecursiveGCD(27, 21) −

Output = 27 % 21 = 6

if 6 != 0{
   return findRecursiveGCD(n1: 21, n2: 6)
} else{
   return n2
}

Output = 21 % 6 = 3

if 3 != 0{
   return findRecursiveGCD(n1: 6, n2: 3)
} else{
   return n2
}

Output = 6 % 3 = 0

if 0 != 0{
   return findRecursiveGCD(n1: 6, n2: 3)
} else{
   return n2 = 3
}

So the GCD of 27 and 21 is 3.

Updated on: 13-Dec-2022

279 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements