π‘ Problem Formulation: A Harshad number, or Niven number, is an integer that is divisible by the sum of its own digits when written in base 10. The challenge is to implement a Python program that takes a number as input and returns a boolean indicating whether it is a Harshad number. For instance, the input 18 should return True
as 1+8=9 and 18 is divisible by 9.
Method 1: Traditional Iterative Approach
This method involves iterating over each digit of the number, computing the sum of these digits, and checking if the original number is divisible by this sum. It makes use of basic arithmetic operations and control structures in Python.
Here’s an example:
def is_harshad(number): original_number = number digits_sum = 0 while number > 0: digits_sum += number % 10 number //= 10 return original_number % digits_sum == 0 print(is_harshad(18))
Output: True
This snippet defines a function is_harshad()
that computes the sum of the digits of the given number and checks if the number is divisible by this sum using the modulus operator. It returns the result as a boolean value.
Method 2: Using String Conversion
Leverages the ability to convert a number to a string for easy digit extraction and sums up the digits through iteration or a functional programming approach like map()
.
Here’s an example:
def is_harshad(number): digits_sum = sum(map(int, str(number))) return number % digits_sum == 0 print(is_harshad(18))
Output: True
The code snippet converts the number to a string to get the individual digits easily, then maps each character back to an integer and sums them up. The divisibility check is the same as in Method 1.
Method 3: List Comprehension
Implements a more Pythonic approach using list comprehension to create a list of digits from the number and then summing them up.
Here’s an example:
def is_harshad(number): digits_sum = sum([int(digit) for digit in str(number)]) return number % digits_sum == 0 print(is_harshad(18))
Output: True
Here, the function generates a list of digits using list comprehension and sums this list. It then returns the result of the divisibility check.
Method 4: Recursive Approach
This method employs a recursive function to calculate the sum of digits and determine if a number is a Harshad number. It’s an elegant solution but may not be as straightforward as the iterative approaches.
Here’s an example:
def digits_sum(number): return number if number < 10 else number % 10 + digits_sum(number // 10) def is_harshad(number): return number % digits_sum(number) == 0 print(is_harshad(18))
Output: True
The code defines a recursive function, digits_sum()
, that finds the sum of digits of a number and another function, is_harshad()
, that uses this sum to check for Harshad number property.
Bonus One-Liner Method 5: Functional Programming Perspective
This concise one-liner uses a combination of Pythonβs functional programming features along with list comprehension to achieve the result in a single line of code.
Here’s an example:
print((lambda n: n % sum(int(d) for d in str(n)) == 0)(18))
Output: True
This snippet involves a lambda function that encapsulates the entire logic of checking for Harshad number in one line. It computes the sum of digits using a generator expression inside the sum function and immediately evaluates the divisibility.
Summary/Discussion
- Method 1: Traditional Iterative Approach. Easy to understand. Inefficient for very large numbers as it involves many iterations.
- Method 2: Using String Conversion. More concise. Still involves iteration internally but hides complexity.
- Method 3: List Comprehension. Pythonic and readable. May be slightly less efficient due to the creation of a list.
- Method 4: Recursive Approach. Elegant and compact. Could lead to a maximum recursion depth error for very large numbers.
- Method 5: One-Liner. Great for quick scripts and demonstrates functional programming. Might be harder to read for those unfamiliar with lambdas and comprehension.