In this tutorial, we will learn how to check if a number is a strong number in Swift. We will understand the concept of a strong number, write an algorithm to determine it, and implement the solution step by step with a complete Swift program and output.


What is a Strong Number?

A strong number is a number in which the sum of the factorials of its digits equals the original number. For example:

  • 145 is a strong number because 1! + 4! + 5! = 145.
  • 2 is a strong number because 2! = 2.
  • 123 is not a strong number because 1! + 2! + 3! ≠ 123.

We will now write an algorithm to determine if a number is a strong number.


Algorithm to Check Strong Number

  1. Take the number as input.
  2. Extract each digit from the number:
    1. Calculate the factorial of the digit.
    2. Add the factorial to a running sum.
  3. Compare the sum with the original number.
  4. If they are equal, the number is a strong number; otherwise, it is not.

Next, we will implement this algorithm step by step in Swift, starting with helper functions and testing the implementation.


Step-by-Step Implementation in Swift

1. Write a Function to Calculate Factorial

Create a helper function that calculates the factorial of a given number:

</>
Copy
func factorial(_ n: Int) -> Int {
    if n == 0 || n == 1 {
        return 1 // Base case: 0! and 1! = 1
    }
    return n * factorial(n - 1) // Recursive case
}

2. Extract Digits and Calculate the Strong Number Sum

Write the main function to extract digits, calculate the factorial of each digit, and compute the sum:

</>
Copy
func isStrongNumber(_ number: Int) -> Bool {
    var num = number           // Copy of the input number
    var sum = 0                // Initialize the sum
    
    while num > 0 {
        let digit = num % 10   // Extract the last digit
        sum += factorial(digit) // Add the factorial of the digit to the sum
        num /= 10              // Remove the last digit
    }
    
    return sum == number       // Check if the sum equals the original number
}

Explanation:

let digit = num % 10: Extracts the last digit of the number.

sum += factorial(digit): Adds the factorial of the digit to the running total.

num /= 10: Removes the last digit from the number.

return sum == number: Compares the sum of factorials with the original number to determine if it is a strong number.

3. Test the Function

Let’s test the function with some example inputs:

</>
Copy
// Test cases
print("145 is a strong number: \(isStrongNumber(145))") // true
print("2 is a strong number: \(isStrongNumber(2))")     // true
print("123 is a strong number: \(isStrongNumber(123))") // false
print("1 is a strong number: \(isStrongNumber(1))")     // true

The function correctly identifies whether the numbers are strong numbers or not.

Complete Swift Program

Here’s the complete Swift program to check if a number is a strong number:

main.swift

</>
Copy
import Foundation

// Function to calculate factorial
func factorial(_ n: Int) -> Int {
    if n == 0 || n == 1 {
        return 1 // Base case: 0! and 1! = 1
    }
    return n * factorial(n - 1) // Recursive case
}

// Function to check if a number is a strong number
func isStrongNumber(_ number: Int) -> Bool {
    var num = number           // Copy of the input number
    var sum = 0                // Initialize the sum
    
    while num > 0 {
        let digit = num % 10   // Extract the last digit
        sum += factorial(digit) // Add the factorial of the digit to the sum
        num /= 10              // Remove the last digit
    }
    
    return sum == number       // Check if the sum equals the original number
}

// Test cases
print("145 is a strong number: \(isStrongNumber(145))") // true
print("2 is a strong number: \(isStrongNumber(2))")     // true
print("123 is a strong number: \(isStrongNumber(123))") // false
print("1 is a strong number: \(isStrongNumber(1))")     // true

Output:

145 is a strong number: true
2 is a strong number: true
123 is a strong number: false
1 is a strong number: true

Screenshot from Xcode

Swift Program to Check Strong Number