ハーシャッド数 ---パイセップ--- 「ハーシャッド数」問題の詳細なガイドと __PYTERM_0__ 実装。 ---パイセップ--- 正の整数 __PYCODE_1__ を受け取り、__PYCODE_3__ がハーシャッド (ニーブン) 数の場合は __PYCODE_2__、それ以外の場合は __PYCODE_4__ を返す関数 __PYCODE_0__ を作成します。ハーシャッド数は、その桁の合計で割り切れる数です。たとえば、1 + 8 = 9 であり、18 は 9 で割り切れるため、18 はハーシャッド数です。 ---パイセップ--- __PYTERM_0__ 基本 ---パイセップ--- 基本 ---パイセップ--- 「ハーシャッド数」問題は、基礎セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- ハーシャッド数のロジックフローを可視化します。 ---パイセップ--- ハーシャッド数の問題文を注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。 ---パイセップ--- 実稼働標準に合わせてコードをクリーンアップします。 ---パイセップ--- 空の入力構造体 ---パイセップ--- 単一要素入力 ---パイセップ--- 大きな数値限界 ---パイセップ--- 基本アプローチのロジックを説明してください。 ---パイセップ--- null または空の入力などの特殊なケースについて説明します。 ---パイセップ--- 標準の基本問題のプロパティが適用されます。 ---パイセップ--- セットやヒープなどの Basics 固有のデータ構造の使用を検討してください。 ---パイセップ--- 豊富な数 ---パイセップ--- 「豊富な数」問題の詳細なガイドと __PYTERM_0__ の実装。 ---パイセップ--- 正の整数 __PYCODE_1__ を受け取り、__PYCODE_3__ が豊富な数の場合は __PYCODE_2__ を、そうでない場合は __PYCODE_4__ を返す関数 __PYCODE_0__ を作成します。豊富な数とは、適切な約数 (数自体を除くすべての約数) の合計がその数より大きい数です。 ---パイセップ--- __PYTERM_0__ 基本 ---パイセップ--- 基本 ---パイセップ--- 「豊富な数」の問題は、基礎セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- 豊富な数のロジックフローを可視化します。 ---パイセップ--- 豊富な数の問題文をよく読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。
Detailed guide and Python implementation for the 'Harshad number' problem.
1. 学ぶ
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.
問題提起
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
例
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
解決する準備はできましたか?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
インタビューの洞察とバリエーション
複雑さの分析の内訳
なぜ時間がかかるのか: Directly evaluates all possibilities.
なぜ宇宙なのか: Uses standard local memory.
なぜ時間がかかるのか: Optimized paths reduce total operations.
なぜ宇宙なのか: May trade memory for speed.
最適化されたソリューションの Python コード
最適化されたソリューションの Python コード
def is_harshad_opt(n: int) -> bool:
temp, total = n, 0
while temp > 0:
total += temp % 10
temp //= 10
return n % total == 0ブルート フォース コード (スポイラーガード付き)
ブルート フォース コード (スポイラーガード付き)
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.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
Key Revision Notes
Standard Basics problem properties apply.
関連する質問
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
推奨される Python リソース
関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。
Python ループ
Python ループを使用してデータを反復処理する方法を学びます。インタラクティブな例を使用して、for ループ、while ループ、ブレーク、継続、ループのベスト プラクティスをマスターします。
Python で乱数を生成する方法
Python で乱数を生成する方法を学びます。 randrange、randint、およびuniform floatの生成とシード制御を比較します。
Python 文字列メソッドのチートシート
Python 文字列操作の完全なリファレンス ガイド。文字列プロパティの書式設定、検索、分割、置換、チェックをマスターします。
Python と JavaScript: どちらのプログラミング言語が最適ですか?
Python と JavaScript の包括的な比較。構文の違い、パフォーマンス、使用例 (バックエンドとフロントエンド)、およびコーディング例を調べます。