-
Notifications
You must be signed in to change notification settings - Fork 0
Create 50. Pow(x, n).md #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # 50. Pow(x, n) | ||
| ## STEP1 | ||
| - 何も見ずに解いてみる | ||
| - 繰り返し二乗法を書く。 | ||
| - 累乗の部分の書き方次第で OverflowError になる。なぜかは不明。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def myPow(self, x: float, n: int) -> float: | ||
| if n < 0: | ||
| return 1 / self.myPow(x, -n) | ||
|
|
||
| result = 1.0 | ||
| pow = x | ||
| while n > 0: | ||
| if n % 2 == 1: | ||
| result *= pow | ||
| pow *= pow | ||
| # pow **= 2 このようにすると OverflowError | ||
| n //= 2 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. x に倣って n も別変数に置き換えるというのは自分も同意です。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。おっしゃる通り問題はないのですが読みやすさ的には課題がありますね。 |
||
| return result | ||
| ``` | ||
|
|
||
| ## STEP2 | ||
| ### プルリクやドキュメントを参照 | ||
| - https://docs.python.org/3/library/functions.html#pow | ||
| - pow 組み込み関数にあるので変数名は変えた方が良さそう。 | ||
| - https://github.com/olsen-blue/Arai60/pull/45/files | ||
| - 再帰でも書ける。 | ||
|
|
||
| 再帰で書いてみる。 | ||
| ```python | ||
| class Solution: | ||
| def myPow(self, x: float, n: int) -> float: | ||
| if n == 0: | ||
| return 1.0 | ||
| if n < 0: | ||
| return 1.0 / self.myPow(x, -n) | ||
|
|
||
| if n % 2 == 1: | ||
| return x * self.myPow(x * x, n // 2) | ||
| else: | ||
| return self.myPow(x * x, n // 2) | ||
| ``` | ||
| - // で割っているので (n - 1) // 2 とは書かなくて良いかなと思った。 | ||
|
|
||
| - https://github.com/TORUS0818/leetcode/pull/47#discussion_r2038337006 | ||
| - これ読みやすい。n が固定されていて、bit, base という変数名も明確。 | ||
|
|
||
| これを踏まえて書き直し。 | ||
| ```python | ||
| class Solution: | ||
| def myPow(self, x: float, n: int) -> float: | ||
| if n < 0: | ||
| return 1.0 / self.myPow(x, -n) | ||
|
|
||
| result = 1.0 | ||
| bit = 1 | ||
| base = x | ||
| while bit <= n: | ||
| if n & bit: | ||
| result *= base | ||
| base *= base | ||
| bit <<= 1 | ||
| return result | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. '# ex) 3^5 = 3^(2^0 + 2^2) = 3^(2^0) * 3^(2^2)'
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。少なくとも repeated squaring とは書いた方が良さそうですね。 |
||
| ``` | ||
|
|
||
| - n が負の数の場合 n を正にして再度関数を呼ぶパターンはそんなに読みにくさを感じない。明示的に変換を書いている分数学的に同値な変形がわかりやすいのでは。まあ同一関数内で n, x ともに上書きするのでも全然良いとは思う。 | ||
|
|
||
| - https://github.com/hroc135/leetcode/pull/43/files | ||
| - 上記PRに書いてある IEEE 754 に関する部分を読んだ。NaN の表現などについては知らなかった。 | ||
| - https://www.geeksforgeeks.org/computer-organization-architecture/ieee-standard-754-floating-point-numbers/ | ||
|
|
||
| ## STEP3 | ||
| ### 3回ミスなく書く | ||
| ```python | ||
| class Solution: | ||
| def myPow(self, x: float, n: int) -> float: | ||
| if n < 0: | ||
| return 1.0 / self.myPow(x, -n) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. x=0をケアするとより良いかと思います。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ありがとうございます。組み込み関数では以下のようになっていました。 >> pow(0, -1) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 組み込み関数でそもそもエラーを出すので特段ケアする必要はないのですね。失礼しました。 |
||
|
|
||
| result = 1.0 | ||
| base = x | ||
| bit = 1 | ||
| while bit <= n: | ||
| if bit & n: | ||
| result *= base | ||
| base *= base | ||
| bit <<= 1 | ||
| return result | ||
| ``` | ||
|
|
||
| 1分,1分,1分で3回Accept | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
後の step でコメントありますが、
powだと built-in と被るのと、英単語としてもbaseとかのほうがよいかなと感じました。https://docs.python.org/ja/3.13/library/functions.html#pow