swift

find-strong-numbers-range()

Parameters: start: Int, end: Int

Starting and ending integers for the desired range

Returns: Array of integers within the range that are strong numbers

The function ‘find-strong-numbers-range’ accepts two integer parameters, defining a range, and identifies all strong numbers within that range. A strong number is a number in which the sum of the factorials of its digits is equal to the number itself.

variables
control flow - loops
conditional statements - if statement
functions
arithmetic operators
Medium dificulty

Crafting a Swift Function to Find Strong Numbers in a Range

Hello there, esteemed programmer! Our journey in the realm of programming takes us to the exploration of a fascinating function in Swift: find-strong-numbers-range. This function, rooted in number theory, allows for detecting if a number is a 'strong' number within a specified range. In its simplicity lies an exciting challenge that's worth unraveling! Buckle up, and let's dive right into code!

Step 1: Understanding the Problem

A strong number is a special number whose sum of factorial of digits is equal to the original number. For instance, 145 is a strong number since 1! + 4! + 5! = 145. The goal is to write a function to find such numbers within a given range.


// Initial function declaration

func findStrongNumbersInRange(start: Int, end: Int) -> [Int] {
}

Step 2: Calculate the Factorial

First, we need a helper function to calculate the factorial of a number. This can be done with a simple loop.


// Including the factorial function in our main function

func findStrongNumbersInRange(start: Int, end: Int) -> [Int] {
    func factorial(_ num: Int) -> Int {
        var fact = 1
        for i in 1...num {
            fact *= i
        }
        return fact
    }
}

Step 3: Check if a Number is Strong

Next, we need to check if a number is strong. Convert the number to a string, calculate the factorial for each digit and sum them up. If the sum equals the original number, it is a strong number.


// Adding isStrong function

func findStrongNumbersInRange(start: Int, end: Int) -> [Int] {
    func factorial(_ num: Int) -> Int {...}

    func isStrong(_ num: Int) -> Bool {
        let digits = String(num).compactMap { Int(String($0)) }
        let sum = digits.map { factorial($0) }.reduce(0, +)
        return sum == num
    }
}

Step 4: Finding Strong Numbers in a Range

Finally, we'll iterate through the given range and use the isStrong function to find strong numbers. The numbers will be collected into an array and returned by the function.


// Final Swift function to find strong numbers in a range

func findStrongNumbersInRange(start: Int, end: Int) -> [Int] {
    func factorial(_ num: Int) -> Int {...}

    func isStrong(_ num: Int) -> Bool {...}

    var strongNumbers: [Int] = []
    for i in start...end {
        if isStrong(i) {
            strongNumbers.append(i)
        }
    }
    return strongNumbers
}

Conclusion

This function efficiently finds all the strong numbers in a given range. The helper functions factorial and isStrong assist in breaking the problem into smaller, solvable steps.

Learn function in:

Strong Number Finding

A function that finds strong numbers within a range

Learn more

Mathematical principle

This function relies on the concept of **factorial** - a non-negative number `n`, denoted by `n!`, is the product of all positive integers not greater than `n`. For any number, we calculate the factorial of each digit, sum them up. If the sum is equal to the original number, it's a **strong number**

Learn more