Python GrundlagenEinfach

Harshad-Nummer

Detaillierte Anleitung und Python-Implementierung für das Problem „Harshad-Nummer“.

Problemstellung

Einfach

Schreiben Sie eine Funktion is_harshad(n), die eine positive Ganzzahl n annimmt und True zurückgibt, wenn n eine Harshad-Zahl (Niven) ist, andernfalls False. Eine Harshad-Zahl ist eine Zahl, die durch die Summe ihrer Ziffern teilbar ist. Beispielsweise ist 18 eine Harshad-Zahl, da 1 + 8 = 9 und 18 durch 9 teilbar ist.

Einschränkungen
  • 1 <= n <= 10^9

Beispiele

Example 1
Input
n = 18
Output
True
Explanation

Sum of digits = 1 + 8 = 9. Since 18 % 9 == 0, it is a Harshad number.

Example 2
Input
n = 21
Output
True
Explanation

Sum of digits = 2 + 1 = 3. Since 21 % 3 == 0, it is a Harshad number.

Example 3
Input
n = 14
Output
False
Explanation

Sum of digits = 1 + 4 = 5. Since 14 % 5 != 0, it is not a Harshad number.

Need a Hint?
Erwägen Sie die Verwendung von Basics-spezifischen Datenstrukturen wie Mengen oder Heaps.
Edge Cases to Watch
  • Leere Eingabestrukturen
  • Einzelelementeingaben
  • Große numerische Grenzen

Bereit zur Lösung?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Im Editor öffnen
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Empfohlene Python-Ressourcen

Erweitern Sie Ihr Wissen mit zugehörigen interaktiven Tutorials, Spickzetteln und Codevergleichen.