1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「中国剰余定理と線形代数」様を、sympyでAI先生に教えてもらいました。

1
Last updated at Posted at 2026-06-27

・2022年にChatGPTは無かったのかしら。です。次行は私の、

オリジナル

@kenjihiranabe(平鍋 健児) 様へ。ありがとうございます。

(本日の)ChatGPT先生へ

問題
3で割ると2余り、5で割ると3余り、7で割ると 2余るような数は何か?
:::note
**問題**
$3$ で割ると$2$余り、$5$ で割ると $3$ 余り、$7$ で割ると $2$ 余るような数は何か?
:::

(表示省略)

上記をsympyで教えて下さい。

(途中抜粋)

from sympy.ntheory.modular import crt

# 法
m = [3, 5, 7]

# 余り
a = [2, 3, 2]

x, lcm = crt(m, a)

print("x =", x)
print("周期 =", lcm)
print("一般解: x =", x, "+", lcm, "*n")

実行結果

x = 23
周期 = 105
一般解: x = 23 + 105*n

確認

from sympy.ntheory.modular import crt

m = [3, 5, 7]
a = [2, 3, 2]

x, lcm = crt(m, a)

print("最小解 =", x)

print(x % 3)
print(x % 5)
print(x % 7)

実行結果

最小解 = 23
2
3
2

一般解を数式として表示

from sympy import symbols
from sympy.ntheory.modular import crt

n = symbols('n', integer=True)

x0, period = crt([3,5,7], [2,3,2])

x = x0 + period*n

print(x)

出力

105*n + 23

...crt() は 中国剰余定理 (Chinese Remainder Theorem) を実装した SymPy の標準関数であり、合同式の連立方程式を効率よく解くことができます。

いつもと違うおすすめです。

Qiita内

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?