Python math.isqrt() メソッド

Python math 模块Python math モジュール

Python math.isqrt(x)メソッドは x の平方根を返し、平方根を最も近い整数に切り捨てます。

数値は 0 以上である必要があります。

Python バージョン: 3.8

構文

math.isqrt() メソッドの構文は次のとおりです:

math.isqrt(x)

パラメータの説明:

  • x-- 必須、数値。x が数値でない場合は TypeError を返します。数値が 0 未満の場合は ValueError を返します。

戻り値

数値の平方根を表す整数 int を返し、平方根を最も近い整数に切り捨てます。

例

次の例は、数値の平方根を返し、平方根を最も近い整数に切り捨てます:

例

# math モジュールをインポート
import math

# 平方根を出力
# Print the square root of different numbers
print (math.sqrt(10))
print (math.sqrt (12))
print (math.sqrt (68))
print (math.sqrt (100))

# 平方根を出力し、平方根を最も近い整数に切り捨てます
print (math.isqrt(10))
print (math.isqrt (12))
print (math.isqrt (68))
print (math.isqrt (100))

出力結果:

3.1622776601683795
3.4641016151377544
8.246211251235321
10.0
3
3
8
10

Python math 模块Python math モジュール

その他の拡張