swift
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.
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!
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.
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
}
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
}
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.
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
}
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