適用前に確認
実際の動作を期待する結果と比較します。検証に失敗した場合は、次の変更を加える前に原因を調べてください。
def mod_power(base, exponent, modulus):
if any(type(v) is not int for v in (base, exponent, modulus)):
raise ValueError("operand.integer")
if exponent < 0 or modulus <= 0:
raise ValueError("operand.range")
result = 1 % modulus
base %= modulus
while exponent:
if exponent & 1:
result = result * base % modulus
base = base * base % modulus
exponent >>= 1
return result実際の動作を期待する結果と比較します。検証に失敗した場合は、次の変更を加える前に原因を調べてください。
OpenAI互換またはAnthropicのエンドポイントを、ベースURLと必要なAPIキーで設定し、接続をテストします。ローカルのモデルサーバーは起動が必要です。リモートツールは別途データを送信する場合があります。
負の底、指数ゼロ、法1を確認し、負の指数、非整数、真偽値は乗算前に拒否します。
def mod_power(base, exponent, modulus):
if any(type(v) is not int for v in (base, exponent, modulus)):
raise ValueError("operand.integer")
if exponent < 0 or modulus <= 0:
raise ValueError("operand.range")
result = 1 % modulus
base %= modulus
while exponent:
if exponent & 1:
result = result * base % modulus
base = base * base % modulus
exponent >>= 1
return result