swift

find-narcissistic-numbers-range()

Parameters: (start: Int, end: Int)

Start and end of the range to search for narcissistic numbers

Returns: Array of narcissistic numbers within specified range

A practical function that discovers all the narcissistic numbers within a provided range. Implementation in Swift enhances problem-solving and mathematical abilities.

Loops
Conditional Statements
Mathematical Operators
Functions
Variable Declaration
Medium dificulty

Creating a Swift Function to Find Narcissistic Numbers in a Specified Range

Hello programmer, welcome to this blog post. Here, we will delve into crafting a Swift-based function that identifies narcissistic numbers within a specific range. Fear not, it's simpler than it sounds. This walkthrough will take you through each step, offering explanations and insights. Strap in and code on!

Step 1: Understand the Problem

A narcissistic number is a number that is the sum of its own digits each raised to the power of the number of digits. For example, 153 is a narcissistic number because 1³ + 5³ + 3³ = 153. The goal of the function find-narcissistic-numbers-range is to return a list of all narcissistic numbers in a given range. This involves iterating over the range, checking whether each number is narcissistic, and adding it to the result list if it is.

Step 2: Plan the Solution

First, we'll need to create a helper function to check whether a number is narcissistic. This function will convert the number to a string (to easily get the number of digits), map each digit to its power raised to the length, and sum them up to see if it equals the original number.

Here is a starting code snippet:

func isNarcissistic(num: Int) -> Bool {
    let strNum = String(num)
    let length = strNum.count
    let sum = strNum.reduce(0) { $0 + Int(pow(Double(String($1))!, Double(length))) }
    return sum == num
}

Step 3: Implement the Main Function

The main function will iterate over the range, check if the number is narcissistic using the helper function, and add it to the list if it is.

func findNarcissisticNumbers(range: Range<Int>) -> [Int] {
    var narcissisticNumbers = [Int]()

    for num in range {
        if isNarcissistic(num: num) {
            narcissisticNumbers.append(num)
        }
    }

    return narcissisticNumbers
}

Step 4: Test and Refine the Solution

Next, test the function with various ranges to ensure it works correctly. The narcissistic numbers in the range 1 to 500 are 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, and 407.

Conclusion

This function provides a swift implementation for finding all narcissistic numbers within a given range. It utilizes a helper function to determine whether a number is narcissistic by analyzing its digits and then uses this helper within the main function to identify and accumulate all narcissistic numbers within the range.

Here is the final code:

func isNarcissistic(num: Int) -> Bool {
    let strNum = String(num)
    let length = strNum.count
    let sum = strNum.reduce(0) { $0 + Int(pow(Double(String($1))!, Double(length))) }
    return sum == num
}

func findNarcissisticNumbers(range: Range<Int>) -> [Int] {
    var narcissisticNumbers = [Int]()

    for num in range {
        if isNarcissistic(num: num) {
            narcissisticNumbers.append(num)
        }
    }

    return narcissisticNumbers
}

Learn function in:

Finding Narcissistic Numbers

Identifying all numbers in a range that are 'narcissistic'

Learn more

Mathematical principle

A narcissistic number in a given number base is a number that is the sum of its own digits each raised to the power of the count of digits. The function iterates through a number range, raising each digit of each number to the power of the count of digits, and checks if the sum equals the given number. For instance, `153` is a narcissistic number because `1^3 + 5^3 + 3^3 = 153`.

Learn more