Python PodstawyŁatwe

Numer Harshada

Szczegółowy przewodnik i implementacja Python dla problemu „liczby Harshada”.

Oświadczenie o problemie

Łatwe

Napisz funkcję is_harshad(n), która przyjmuje dodatnią liczbę całkowitą n i zwraca True, jeśli n jest liczbą Harshada (Niven), lub False w przeciwnym razie. Liczba Harshada to liczba podzielna przez sumę jej cyfr. Na przykład 18 jest liczbą Harshada, ponieważ 1 + 8 = 9, a 18 jest podzielne przez 9.

Ograniczenia
  • 1 <= n <= 10^9

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla podstaw, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.