2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

%を使わずに剰余算

Posted at

行列プログラマーのp.19

Pythonを用いて、2304811 を47で割った余りを求めよ。ただし%を使用しないこと。

%を使わないでどうやってやれって言うんだ!?と思ってハマったのでメモ。

answer.py
2304811 - (2304811 // 47) * 47

Python には // という演算子があって、これは切り捨て除算と呼ばれる。

sample.py
print(10 / 3)      # 3.33333333333333
print(10 // 3)     # 3
print(10 // 3.3)   # 3.0
print(10 % 3.3)    # 0.10000000000000053
print(10 // 3.4)   # 2.0
print(10 // 3.33)  # 3.0

Cから入った人間だと、整数値を整数値で割れば、切り捨て除算じゃないか。と思ってたけど、型がない世界だと、そうは行かない模様。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?