swift

findHarshadNumbersRange()

Parameters: start:Int, end:Int

Start and end of the range within which to find the Harshad numbers.

Returns: Array of Int - All Harshad numbers between start and end.

The function findHarshadNumbersRange takes a range of integers as input and iterates through the numbers, calculating the sum of their digits and determining if each number is a Harshad number—a number divisible by the sum of its digits.

loops
modulus operator
integer division
arrays
function definition
range
Medium dificulty

Crafting a Swift Function to Find Harshad Numbers in a Range

Hello programmer! Welcome to this blog post. We're keeping it simple and clear today. Look forward to learning how to create a function in Swift that's all about finding Harshad numbers in given range. Remember, Harshad number is an integer that is divisible by the sum of its digits. Doesn't that sound interesting? Stay tuned to walk through the steps below.

Step 1: Understand the Problem

We are required to generate a list of Harshad numbers in a given range. A Harshad number is an integer that is divisible by the sum of its digits. For instance, the number 18 is a Harshad number because the sum of its digits (1+8) is 9, and 18 itself is divisible by 9.

Let's start coding the solution in Swift.

func findHarshadNumbers(inRange range: ClosedRange<Int>) -> [Int] {
  var harshadNumbers: [Int] = []

  //... upcoming steps will fill this array

  return harshadNumbers
}

Step 2: Loop through the Range

We need to check every number in the given range, to see whether it's a Harshad number or not. For that, we will use a for-in loop.

for number in range {
  //... upcoming steps will check if 'number' is a Harshad number
}

Step 3: Calculate Sum of Digits

To calculate the sum of the digits of a number, we can convert the number to a string, loop over each character (digit), convert it back to an integer, and sum them up.

let sum = String(number).reduce(0) { $0 + Int(String($1))! }

Step 4: Check if the Number is a Harshad number

A number is Harshad if it is divisible by the sum of its digits. In Swift, we can use the modulus operator (%) to find if there is a remainder when the number is divided by the sum. If there isn't (i.e., the remainder is zero), the number is a Harshad number.

if number % sum == 0 {
  harshadNumbers.append(number)
}

Step 5: Return the Result

Finally, after the loop is finished, our function return the array of Harshad numbers.

return harshadNumbers

Conclusion

Here's the completed Swift function that finds all Harshad numbers in a given range:

func findHarshadNumbers(inRange range: ClosedRange<Int>) -> [Int] {
  var harshadNumbers: [Int] = []
  for number in range {
    let sum = String(number).reduce(0) { $0 + Int(String($1))! }
    if number % sum == 0 {
      harshadNumbers.append(number)
    }
  }
  return harshadNumbers
}

This function works by systematically checking each number in the provided range to see if it meets the conditions to be a Harshad number: that is, if the number is divisible by the sum of its digits. Note the use of Swift's built-in reduce function, which is a powerful tool for calculating sums and other aggregate calculations.

Learn function in:

Harshad Number

A number is a Harshad Number if it is divisible by the sum of its digits.

Learn more

Mathematical principle

Harshad number, also known as a Niven number, is a number that is divisible by the sum of its digits. For example, 18 is a Harshad number because 18 is divisible by 9 (1+8). This function implements the concept of Harshad numbers, iterating each number in a given range and checks whether it's a Harshad number using the `number % sum_of_digits == 0` formula.

Learn more