Skip to content

Conversation

hroc135
Copy link
Owner

@hroc135 hroc135 commented Dec 24, 2024

`if k > n/2 { k = n - k }`としていた。
こっちの方がシンプルだし関数呼び出しを減らせる(数nsくらいなのでそこまで影響は大きくない)
- あと自分のコードは割り切れない時に割ってしまうと数字がずれてしまうことを懸念して逐次確認するようにしていたが、ライブラリのコードではその確認をしていない。
分母で素数pが出現するまでにループはp回回る。分子の積の一番小さい数n-k+1と、素数pを含む最も小さい数の差はp以下である。よって、必ず割り切れる、という理屈
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(すべてコードで表現せずに)コメントで一言書いておくというのは一つです。
逆に、割り切れることがほとんど起きないならば、溢れるのではないかという心配をする人もいるかもしれないですよね。

- 以前川中さんが「テストケースは機械的に生成するといい」とおっしゃっていたので、以下を試す
- (m,n) = (0,0), (0,1), (1,0), (1,1), (1,2), (2,1), (2,2)
- 2つほど境界条件関連のバグが見つかった。
脳内シミュレーションの練習にもなるので習慣化しよう
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これはとても大事です。

テストケースを機械的に作って脳内シミュレーションをしたあと、すべての行を通っているか、みたいなものも補助的に使っています。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すべての行を通っているか

この視点はなかったです

- そういえば高校か中学の組み合わせの授業でやった記憶がある
- 以下のコードでinteger overflowを起こしてWA
- 不用意に大きい数の積を計算するべきではない
- レベル1: integer overflowって何?小さい数だと正解なのに大きい数だと不正解なのはなぜ?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int の最大値は、 64 ビット実装の場合は 9 * 10^18 くらいです。大きな数字を扱う場合は、この数字を念頭に置いて扱うとよいと思います。

余談ですが、金額の計算を double でやって、計算誤差で端数が合わなくなったという話を思い出しました。 IEEE754 についても調べておくとよいかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IEEE754 について聞いたこともなかったのですが、高校の情報レベルだったのですね、、
https://www.youtube.com/watch?v=42xVgTaIKGo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants