Harshad number
Detailed guide and Python implementation for the 'Harshad number' problem.
1. Concept Overview
The 'Harshad number' problem is a key challenge in the Basics section.
This implementation focuses on easy-level logic in Python.
We prioritize technical accuracy and code readability in our provided solutions.
2. Real-World Applications
3. Visual Intuition
Visualizing the logic flow for Harshad number.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Harshad number carefully.
2. Formulate brute force
Draft a simple iterative solution.
3. Identify inefficiency
Look for redundant calculations.
4. Optimize search path
Use hashing or sorting to speed up the process.
5. Final Implementation
Clean up the code for production standards.
Problem Statement
Write a function is_harshad(n) that takes a positive integer n and returns True if n is a Harshad (Niven) number, or False otherwise. A Harshad number is a number that is divisible by the sum of its digits. For example, 18 is a Harshad number because 1 + 8 = 9, and 18 is divisible by 9.
- •1 <= n <= 10^9
Examples
n = 18
True
Sum of digits = 1 + 8 = 9. Since 18 % 9 == 0, it is a Harshad number.
n = 21
True
Sum of digits = 2 + 1 = 3. Since 21 % 3 == 0, it is a Harshad number.
n = 14
False
Sum of digits = 1 + 4 = 5. Since 14 % 5 != 0, it is not a Harshad number.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Interview Insights & Variations
Complexity Analysis Breakdown
Why Time: Directly evaluates all possibilities.
Why Space: Uses standard local memory.
Why Time: Optimized paths reduce total operations.
Why Space: May trade memory for speed.
Optimized Solution Python Code
Optimized Solution Python Code
def is_harshad_opt(n: int) -> bool:
temp, total = n, 0
while temp > 0:
total += temp % 10
temp //= 10
return n % total == 0Brute Force Code (Spoiler Guarded)
Brute Force Code (Spoiler Guarded)
def is_harshad_brute(n: int) -> bool:
total = sum(int(d) for d in str(n))
return n % total == 0Algorithm Pattern Checklist
When dealing with Basics data patterns.
Core Prerequisites
Revision Key Notes
Common Mistakes & Pitfalls
Related Questions
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
How to Generate Random Numbers in Python
Learn how to generate random numbers in Python. Compare randrange, randint, and uniform float generation with seeding control.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.