Python Nozioni di baseFacile

Numero di Harshad

Guida dettagliata e implementazione Python per il problema del "numero di Harshad".

Dichiarazione del problema

Facile

Scrivi una funzione is_harshad(n) che accetta un intero positivo n e restituisce True se n è un numero di Harshad (Niven), o False altrimenti. Un numero di Harshad è un numero divisibile per la somma delle sue cifre. Ad esempio, 18 è un numero di Harshad perché 1 + 8 = 9 e 18 è divisibile per 9.

Vincoli
  • 1 <= n <= 10^9

Esempi

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?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Basics come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.