Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elementary Number Theory with Python

HayaoSuzuki
October 12, 2019

Elementary Number Theory with Python

PyCon mini Hiroshima 2019

HayaoSuzuki

October 12, 2019
Tweet

More Decks by HayaoSuzuki

Other Decks in Science

Transcript

  1. Python Ͱָ͠Ήॳ౳੔਺࿦
    Hayao Suzuki
    PyCon mini Hiroshima 2019
    October 12, 2019

    View full-size slide

  2. Contents
    1 ࣗݾ঺հ
    2 ॳ౳੔਺࿦ͱ͸
    3 ϐλΰϥε਺
    4 2 ͭͷฏํ਺ͷ࿨ͰදͤΔૉ਺
    5 ·ͱΊ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 2 / 39

    View full-size slide

  3. ࣗݾ঺հ
    ͓લ୭Α
    ໊લ Hayao Suzukiʢླ໦ɹॣʣ
    Twitter @CardinalXaro
    ϒϩά https://xaro.hatenablog.jp/
    ઐ໳ ਺ֶ (૊߹ͤ࿦ɾάϥϑཧ࿦)
    ֶҐ म࢜ʢ޻ֶʣ
    ɺిؾ௨৴େֶ
    ࢓ࣄ גࣜձࣾΞΠϦοδͰ Python ϓϩάϥϚΛ͍ͯ͠Δ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 3 / 39

    View full-size slide

  4. ࣗݾ঺հ
    ٕज़ॻͷࠪಡ
    › ʰEffective Pythonʱ
    ʢΦϥΠϦʔδϟύϯʣ
    › ʰNumPy ʹΑΔσʔλ෼ੳೖ໳ʱ
    ʢΦϥΠϦʔδϟύϯʣͳͲ
    › https://xaro.hatenablog.jp/ ʹҰཡ͋Γ·͢ɻ
    ͍ΖΜͳൃද
    › ʮSymPy ʹΑΔ਺ࣜॲཧʯ
    ʢPyCon JP 2018ʣͳͲ
    › https://xaro.hatenablog.jp/ ʹҰཡ͋Γ·͢ɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 4 / 39

    View full-size slide

  5. ॳ౳੔਺࿦ͱ͸
    ੔਺࿦ͱ͸
    ਺ͷੑ࣭ʹؔ͢Δ਺ֶͷҰ෼໺
    ॳ౳੔਺࿦ͱ͸
    ୅਺తͳख๏΍ղੳతͳख๏Λ࢖Θͳ͍਺࿦
    ॳ౳͔ͩΒ؆୯Ͱ͢Ͷʁ Α͔ͬͨʂ
    ॳ౳ͱ͸ಛผͳ༧උ஌͕͍ࣝΒͳ͍͜ͱɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 5 / 39

    View full-size slide

  6. ࠓ೔ͷൃද
    ͳͥ Python Λ࢖͏ͷ͔
    › ൺֱతૉ௚ʹϓϩάϥϛϯάͰ͖Δ
    › ඪ४ϥΠϒϥϦ΋֎෦ϥΠϒϥϦ΋๛෋ʹ͋Δ
    › Guido van Rossum ࢯ͸ΞϜεςϧμϜେֶͰ਺ֶͱܭࢉػՊֶΛ
    ઐ߈͍ͯͨ͠
    ࠓճ࢖͏΋ͷ
    › Python 3.7.x
    › SymPyʢه߸ܭࢉϥΠϒϥϦɺ਺࿦ϥΠϒϥϦ΋๛෋ʣ
    › Silvermanʰ͸͡Ίͯͷ਺࿦ ݪஶୈ 3 ൛ʱ
    ʢؙળग़൛ʣ
    ʢωλݩʣ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 6 / 39

    View full-size slide

  7. ࠓ೔ͷൃද
    Python Ͱָ͠Ήॳ౳੔਺࿦
    › ఆཧͷ݁ՌΛ Python Ͱϓϩάϥϛϯάͯ͠ΈΑ͏
    › ఆཧͷূ໌͔Β Python Ͱϓϩάϥϛϯάͯ͠ΈΑ͏
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 7 / 39

    View full-size slide

  8. Python Ͱָ͠Ήॳ౳੔਺࿦
    ఆཧͷ݁ՌΛ Python Ͱϓϩάϥϛϯ
    άͯ͠ΈΑ͏
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 8 / 39

    View full-size slide

  9. ϐλΰϥε਺
    ఆٛ (ϐλΰϥε਺)
    a2 + b2 = c2 ͕੒Γཱͭࣗવ਺ͷ૊ (a; b; c) Λϐλΰϥε਺ͱݺͿɻ
    ໨ඪ
    ϐλΰϥε਺Λॏෳͳ͘ɺ΋Εͳ͘ܭࢉ͍ͨ͠ɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 9 / 39

    View full-size slide

  10. ϐλΰϥε਺Λܭࢉ͠Α͏ɿࠜੑฤ
    ࠜੑͰܭࢉͩʂ
    from itertools import product
    for a, b, c in product(range(1, 20), repeat=3):
    if a ** 2 + b ** 2 == c ** 2:
    print(a, b, c)
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 10 / 39

    View full-size slide

  11. ϐλΰϥε਺Λܭࢉ͠Α͏ɿࠜੑฤ
    ࠜੑͰܭࢉͨͧ͠ʂ
    3 4 5
    4 3 5
    5 12 13
    6 8 10
    8 6 10
    8 15 17
    9 12 15
    12 5 13
    12 9 15
    15 8 17
    ॏෳ͕ͨ͘͞Μ͋Δ͠ɺ΋Ε͕͋Δ͔΋͠Εͳ͍ɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 11 / 39

    View full-size slide

  12. ط໿ϐλΰϥε਺
    ఆٛ (ط໿ϐλΰϥε਺)
    ޓ͍ʹૉͰ͋Δϐλΰϥε਺Λط໿ϐλΰϥε਺ͱݺͿɻ
    ط໿ϐλΰϥε਺͚ͩΛੜ੒͢Ε͹ଞͷϐλΰϥε਺͸ੜ੒Ͱ͖Δɻ
    ϐλΰϥε਺ (3; 4; 5) ͔Βผͷϐλΰϥε਺ (6; 8; 10) Λ࡞Δ͜ͱ͕Ͱ
    ͖Δɻ
    ࣍ͳΔ໨ඪ
    ط໿ϐλΰϥε਺Λॏෳͳ͘ɺ΋Εͳ͘ܭࢉ͍ͨ͠ɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 12 / 39

    View full-size slide

  13. ط໿ϐλΰϥε਺Λܭࢉ͠Α͏
    ͪΐͬͱ޻෉ͨ͠
    from itertools import combinations
    from math import gcd
    for a, b, c in combinations(range(1, 50), 3):
    if a ** 2 + b ** 2 == c ** 2 and gcd(a, b) == 1:
    print(a, b, c)
    ͪΐͬͱ޻෉ͨ͠
    › a; b; c ͸ͦΕͧΕҧ͏਺Ͱ͋Δ
    › a; b ͸ޓ͍ʹૉͰ͋Δ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 13 / 39

    View full-size slide

  14. ط໿ϐλΰϥε਺Λܭࢉ͠Α͏
    ط໿ϐλΰϥε਺ͷܭࢉ
    3 4 5
    5 12 13
    7 24 25
    8 15 17
    9 40 41
    12 35 37
    20 21 29
    ॏෳ͸ͳͦ͞͏͕ͩɺ΋Ε͕͋Δ͔΋͠Εͳ͍...ɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 14 / 39

    View full-size slide

  15. ط໿ϐλΰϥε਺Λ΋ͬͱ্ख͘ܭࢉ͠Α͏
    ໋୊
    ط໿ϐλΰϥε਺ (a; b; c) ͸ޓ͍ʹૉͳح਺ s; t(s > t) Λ༻͍ͯ
    a = st; b =
    s2 ` t2
    2
    ; c =
    s2 + t2
    2
    ͱͰ͖Δɻ
    ͭ·Γ...
    ޓ͍ʹૉͳح਺ͷϖΞ (s; t) ͕ط໿ϐλΰϥε਺ (a; b; c) ʹରԠ͢Δɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 15 / 39

    View full-size slide

  16. ط໿ϐλΰϥε਺Λ΋ͬͱ্ख͘ܭࢉ͠Α͏
    ิ୊ (͋ͱͰͬ͘͡Γಡ΋͏)
    ط໿ϐλΰϥε਺ (a; b; c) ͷ a; b ͷҰํ͸ۮ਺Ͱ΋͏Ұํ͸ح਺Ͱ͋Δɻ
    ·ͨɺc ͸ৗʹح਺Ͱ͋Δɻ
    ূ໌ʢ͋ͱͰͬ͘͡Γಡ΋͏ʣ.
    ࣗવ਺ͷ૊ (a; b; c) Λط໿ϐλΰϥε਺ͱ͢Δɻ
    ·ͣɺa; b ͕ڞʹۮ਺ͳΒ͹ɺϐλΰϥε਺ͷఆ͔ٛΒ c ΋ۮ਺ͱͳΓɺ
    (a; b; c) ͸ڞ௨Ҽ਺ 2 Λ࣋ͭ͜ͱʹͳΓԾఆʹ൓͢Δɻ࣍ʹɺa; b ͕ڞ
    ʹح਺ͳΒ͹ɺϐλΰϥε਺ͷఆ͔ٛΒ c ͸ۮ਺ͱͳΔɻ͜ͷͱ͖ɺ
    a = 2x + 1; b = 2y + 1; c = 2z ͱ͢Δͱ
    a2 + b2 = c2 ) 2(x2 + x + y2 + y) + 1 = 2z2
    ͱͳΓໃ६ɻ
    Αͬͯɺa; b ͷ಺ɺҰํ͸ۮ਺Ͱ΋͏Ұํ͸ح਺Ͱ͋Δɻ·ͨɺϐλΰϥ
    ε਺ͷఆ͔ٛΒ c ͸ح਺ͱͳΔɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 16 / 39

    View full-size slide

  17. ط໿ϐλΰϥε਺Λ΋ͬͱ্ख͘ܭࢉ͠Α͏
    ิ୊ (͋ͱͰͬ͘͡Γಡ΋͏)
    ط໿ϐλΰϥε਺ (a; b; c) ʹ͓͍ͯɺa Λح਺ɺb Λۮ਺ͱ͢Δɻ͜ͷͱ
    ͖ɺ(c ` b)(c + b) ͸ޓ͍ʹૉͰ͋Δɻ
    ূ໌ʢ͋ͱͰͬ͘͡Γಡ΋͏ʣ.
    ࣗવ਺ͷ૊ (a; b; c) Λط໿ϐλΰϥε਺ͱ͠ɺa Λح਺ɺb Λۮ਺ͱ͢
    ΔɻԾఆ͔Βɺa2 = c2 ` b2 = (c ` b)(c + b) ͱͰ͖Δɻ
    c ` b; c + b ͷڞ௨Ҽ਺Λ d ͱ͢Δͱɺd ͸
    (c + b) + (c ` b) = 2c; (c + b) ` (c ` b) = 2b ΋ׂΓ੾ΔɻԾఆ
    ͔Β b; c ͸ط໿ͳͷͰ d ͸ 1 ·ͨ͸ 2 Ͱ͋Δɻ͔͠͠ɺ
    (c ` b)(c + b) = a2 Ͱ͋Γɺa ͸ح਺ͳͷͰ d ͸ 1 ʹଞͳΒͳ͍ɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 17 / 39

    View full-size slide

  18. ط໿ϐλΰϥε਺Λ΋ͬͱ্ख͘ܭࢉ͠Α͏
    ิ୊ (͋ͱͰͬ͘͡Γಡ΋͏)
    ط໿ϐλΰϥε਺ (a; b; c) ʹ͓͍ͯɺa Λح਺ɺb Λۮ਺ͱ͢Δɻ͜ͷͱ
    ͖ɺc ` b; c + b ͸ͦΕͧΕฏํ਺ͱͳΔɻ
    ূ໌ʢ͋ͱͰͬ͘͡Γಡ΋͏ʣ.
    ࣗવ਺ͷ૊ (a; b; c) Λط໿ϐλΰϥε਺ͱ͠ɺa Λح਺ɺb Λۮ਺ͱ͢Δɻ
    Ծఆ͔Βɺa2 = c2 ` b2 = (c ` b)(c + b) ͱͰ͖Δɻc ` b; c + b ͸
    ޓ͍ʹૉͰ͋Γɺ͔ͭ (c ` b)(c + b) = a2 ͳͷͰɺ(c ` b)(c + b) ͸
    ฏํ਺ͱͳΔɻΑͬͯɺc ` b; c + b ͕ͦΕͧΕฏํ਺ͱͳΔɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 18 / 39

    View full-size slide

  19. ط໿ϐλΰϥε਺Λ΋ͬͱ্ख͘ܭࢉ͠Α͏
    ໋୊
    ط໿ϐλΰϥε਺ (a; b; c) ͸ޓ͍ʹૉͳح਺ s; t(s > t) Λ༻͍ͯ
    a = st; b =
    s2 ` t2
    2
    ; c =
    s2 + t2
    2
    ͱͰ͖Δɻ
    ূ໌ʢ͋ͱͰͬ͘͡Γಡ΋͏ʣ.
    ࣗવ਺ͷ૊ (a; b; c) Λط໿ϐλΰϥε਺ͱ͠ɺa Λح਺ɺb Λۮ਺ͱ͢Δɻ
    Ծఆ͔Βɺa2 = c2 ` b2 = (c ` b)(c + b) ͱͰ͖Δɻc ` b; c + b ͸
    ͦΕͧΕฏํ਺ͳͷͰɺc + b = s2; c ` b = t2 ͱͰ͖ΔɻΑͬͯɺ
    c =
    s2 + t2
    2
    ; b =
    s2 ` t2
    2
    ͱͳΔɻ·ͨɺ
    a = q(c ` b)(c + b) = st
    ͱͳΔɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 19 / 39

    View full-size slide

  20. ط໿ϐλΰϥε਺Λ΋ͬͱ্ख͘ܭࢉ͠Α͏
    ্ख͘޻෉ͨ͠
    from itertools import combinations
    from math import gcd
    for t, s in filter(
    lambda x: gcd(*x) == 1,
    combinations(range(1, 10, 2), r=2),
    ):
    print(
    s * t,
    (s ** 2 - t ** 2) // 2,
    (s ** 2 + t ** 2) // 2,
    )
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 20 / 39

    View full-size slide

  21. ط໿ϐλΰϥε਺Λ΋ͬͱ্ख͘ܭࢉ͠Α͏
    ط໿ϐλΰϥε਺Λ΋ͬͱ্ख͘ܭࢉͨ͠
    3 4 5
    5 12 13
    7 24 25
    9 40 41
    15 8 17
    21 20 29
    35 12 37
    45 28 53
    63 16 65
    ॏෳͳ͘ɺ΋Εͳ͘ܭࢉͰ͖ͨʂ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 21 / 39

    View full-size slide

  22. Python Ͱָ͠Ήॳ౳੔਺࿦
    ఆཧͷ݁ՌΛ Python Ͱϓϩάϥϛϯάͯ͠ΈΑ͏
    › ݁ՌΛͦͷ··࣮૷ͯ͠۩ମྫΛ؍࡯͢Δ
    › ҙ֎ͳ๏ଇ͕ݟ͔ͭΔʢ͔΋ʣ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 22 / 39

    View full-size slide

  23. Python Ͱָ͠Ήॳ౳੔਺࿦
    ఆཧͷূ໌͔Β Python Ͱϓϩάϥϛ
    ϯάͯ͠ΈΑ͏
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 23 / 39

    View full-size slide

  24. 2 ͭͷฏํ਺ͷ࿨ͰදͤΔૉ਺
    ఆٛ (੔਺ͷ߹ಉ)
    ੔਺ a; b ͓Αͼ m ʹ͓͍ͯɺa ` b ͕ m ͷഒ਺ͱͳΔͱ͖ɺa; b ͸Λ
    m Λ๏ͱͯ͠߹ಉͱݺͼɺa ” b (mod m) ͱද͢ɻ
    ఆཧ (Fermat ͷ 2 ฏํ࿨ఆཧ)
    حૉ਺ p ͕ 2 ͭͷฏํ਺ͷ࿨ͰදͤΔ͜ͱͷඞཁे෼৚݅͸ p ” 1
    (mod 4) Ͱ͋Δɻ
    2 ͭͷฏํ਺ͷ࿨ͰදͤΔʁ
    p = 97 Λྫʹߟ͑Δɻ
    › 97 = 92 + 42 ͱͰ͖ΔͷͰɺ97 ” 1 (mod 4) Ͱ͋Δɻ
    ʢ؆୯ʣ
    › 97 ” 1 (mod 4) Ͱ͋ΔͷͰɺ97 = 92 + 42 ͱͰ͖Δɻ
    ʢෆࢥٞʣ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 24 / 39

    View full-size slide

  25. 2 ͭͷฏํ਺ͷ࿨ͰදͤΔૉ਺
    ূ໌ (p = x2 + y2 ) p ” 1 (mod 4)).
    حૉ਺ p ͕ 2 ͭͷฏํ਺ͷ࿨ͰදͤΔͳΒ͹ɺp = x2 + y2 ͱͳΔɻp
    ͸ح਺ͳͷͰɺx Λۮ਺ɺy Λح਺ͱͯ͠Α͍ɻ͜ͷͱ͖ɺ
    x = 2m; y = 2n + 1 ͱ͢Δͱ
    p = x2 + y2 = (2m)2 + (2n + 1)2 = 4(m2 + n2 + n) + 1
    ΑΓɺp ͸ 4 Λ๏ͱͯ͠ 1 ͱ߹ಉͰ͋Δɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 25 / 39

    View full-size slide

  26. ূ໌ͷϥϑεέον
    େࡶ೺ͳํ਑
    › ͍͖ͳΓ x2 + y2 = p Λຬͨ͢ (x; y) Λ౰ͯΔͷ͸೉͍͠ɻ
    › x2 + y2 = Mp Λຬͨ͢ (x; y; M) ͸ൺֱత؆୯ʹग़ͤΔͷͰ
    ग़͢ɻ
    › M ͷ஋ΛͲΜͲΜݮΒͯ͠ 1 ʹ͢Δɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 26 / 39

    View full-size slide

  27. ূ໌ͷεέον
    ূ໌ (p ” 1 (mod 4) ) p = x2 + y2) ͷεέον
    1 p Λ 4 Λ๏ͱͯ͠ 1 ͱ߹ಉͳૉ਺ͱ͢Δɻ
    2 A2 + B2 = Mp ͳΔ (A; B; M) Λ୳͢ɻM = 1 ͳΒऴྃɻ
    3 (A; B; M) ͔Β a2 + b2 = Mr; r » M ` 1 ͳΔ (a; b; r) Λ
    ಘΔɻ
    4 A2 + B2 = Mp; a2 + b2 = Mr ͔Β u2 + v2 = rp ΛಘΔɻ
    5 A u; B v; M r ͱͯ͠ 2 ʹ໭Δɻ
    ͜ΕΛແݶ߱Լ๏ͱݺͿɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 27 / 39

    View full-size slide

  28. ূ໌ͷεέον
    ิ୊
    ߹ಉํఔࣜ x2 ” `1 (mod p) ͸ղΛ࣋ͭɻ
    ূ໌ʢఱԼΓʣ.
    ฏํ৒༨ͷ૬ޓ๏ଇ͔Βಋ͚Δɻ
    ͍͍ײ͡ʹ A2 + B2 = Mp ͳΔ (A; B; M) Λ୳͢
    ߹ಉํఔࣜ x2 ” `1 (mod p) ͷղΛ A ͱ͠ɺB = 1 ͱ͢Δͱɺ
    A2 + B2 ͸ p ͷഒ਺Ͱ͋Δɻ·ͨɺM =
    A2 + B2
    p
    ͱͳΔɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 28 / 39

    View full-size slide

  29. A2 + B2 = Mp ͳΔ (A; B; M) Λ୳͢
    ߹ಉํఔࣜ x2 ” `1 (mod p) ͷղΛࢉग़͢ΔʢఱԼΓʣ
    from random import randint
    from sympy.ntheory import isprime, legendre_symbol
    def solve_quadratic(p):
    if not (isprime(p) and p % 4 == 1):
    raise ValueError
    while True:
    a = randint(1, p - 1)
    b = pow(a, (p - 1) // 4, p)
    if legendre_symbol(a, p) == -1 and 1 <= b < p:
    return b
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 29 / 39

    View full-size slide

  30. A2 + B2 = Mp ͳΔ (A; B; M) Λ୳͢
    (A; B; M) Λ୳͢
    def sums_of_two_squares(p):
    """4 Λ๏ͱͯ͠ 1 ͱ߹ಉͳૉ਺Λ 2 ͭͷฏํ਺ͷ࿨Ͱද͢"""
    if not (isprime(p) and p % 4 == 1):
    raise ValueError
    A, B = solve_quadratic(p), 1
    M = divmod(A ** 2 + B ** 2, p)[0]
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 30 / 39

    View full-size slide

  31. a2 + b2 = Mr Λ୳͢
    a2 + b2 = Mr Λ୳͢
    ҎԼͷ৚݅Λຬͨ͢Α͏ʹ a; b ΛબͿɻ
    a ” A (mod M)
    b ” B (mod M)
    `
    1
    2
    M » a; b »
    1
    2
    M
    r ͱ M ͷؔ܎ͷൿີ
    r =
    a2 + b2
    M
    »
    “ M
    2
    ”2
    + “M
    2
    ”2
    M
    =
    M
    2
    < M:
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 31 / 39

    View full-size slide

  32. a2 + b2 = Mr Λ୳͢
    a2 + b2 = Mr Λ୳͢
    from sympy.ntheory.modular import solve_congruence
    def sums_of_two_squares(p):
    """4 Λ๏ͱͯ͠ 1 ͱ߹ಉͳૉ਺Λ 2 ͭͷฏํ਺ͷ࿨Ͱද͢"""
    if not (isprime(p) and p % 4 == 1):
    raise ValueError
    A, B = solve_quadratic(p), 1
    M = divmod(A ** 2 + B ** 2, p)[0]
    a = solve_congruence((A, M), symmetric=True)[0]
    b = solve_congruence((B, M), symmetric=True)[0]
    r = divmod(a ** 2 + b ** 2, M)[0]
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 32 / 39

    View full-size slide

  33. ূ໌ͷεέον
    ิ୊
    (u2 + v2)(A2 + B2) = (uA + vB)2 + (vA ` uB)2
    ূ໌.
    ܭࢉ͢Δ͚ͩɻ
    (uA + vB)2 + (vA ` uB)2
    = (u2A2 + 2uAvB + v2B2) + (v2A2 ` 2vAuB + u2B2)
    = u2A2 + v2B2 + v2A2 + u2B2
    = (u2 + v2)(A2 + B2):
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 33 / 39

    View full-size slide

  34. ૯࢓্͛
    2 ͭͷํఔࣜ
    A2 + B2 = Mp
    a2 + b2 = Mr
    2 ͭͷํఔࣜͷؔ܎
    (a2 + b2)(A2 + B2) = M2rp
    )(aA + bB)2 + (bA ` aB)2 = M2rp
    )
    aA + bB
    M
    !2
    +
    bA ` aB
    M
    !2
    = rp
    r »
    M
    2
    ͳͷͰɺޮ཰Α͘ܭࢉͰ͖Δɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 34 / 39

    View full-size slide

  35. 2 ͭͷฏํ਺ͷ࿨ͰදͤΔૉ਺
    2 ͭͷฏํ਺ͷ࿨ͰදͤΔૉ਺
    def sums_of_two_squares(p):
    A, B = solve_quadratic(p), 1
    M = divmod(A ** 2 + B ** 2, p)[0]
    while True:
    a = solve_congruence((A, M), symmetric=True)[0]
    b = solve_congruence((B, M), symmetric=True)[0]
    r = divmod(u ** 2 + v ** 2, M)[0]
    s = abs((a * A + b * B) // M)
    t = abs((b * A - a * B) // M)
    if r == 1:
    print(f"${s}^2 + {t}^2={p}$")
    return
    else:
    A, B, M = s, t, r
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 35 / 39

    View full-size slide

  36. 2 ͭͷฏํ਺ͷ࿨ͰදͤΔૉ਺
    2 ͭͷฏํ਺ͷ࿨ͰදͤΔૉ਺
    from sympy import primerange
    primes_1_mod_4 = (
    p for p in primerange(1, 100) if p % 4 == 1
    )
    for p in primes_1_mod_4:
    sums_of_two_squares(p)
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 36 / 39

    View full-size slide

  37. 2 ͭͷฏํ਺ͷ࿨ͰදͤΔૉ਺
    2 ͭͷฏํ਺ͷ࿨ͰදͤΔૉ਺
    22 + 12 = 5
    32 + 22 = 13
    42 + 12 = 17
    52 + 22 = 29
    62 + 12 = 37
    52 + 42 = 41
    72 + 22 = 53
    62 + 52 = 61
    82 + 32 = 73
    82 + 52 = 89
    92 + 42 = 97
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 37 / 39

    View full-size slide

  38. Python Ͱָ͠Ήॳ౳੔਺࿦
    ఆཧͷূ໌͔Β Python Ͱϓϩάϥϛϯάͯ͠ΈΑ͏
    › ߏ੒తͳূ໌͸ϓϩάϥϛϯάͰ͖Δʢ͜ͱ΋͋Δʣ
    › ϓϩάϥϛϯάͰ͖Ε͹ূ໌ΛཧղͰ͖Δʢ͔΋͠Εͳ͍ʣ
    શମ૾Λݟ͍ͨ...
    ϓϩάϥϜશମ͸ https://github.com/HayaoSuzuki/
    PyCon-mini-Hiroshima-2019/blob/master/20191012note.ipynb ʹ
    ͋Γ·͢ɻ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 38 / 39

    View full-size slide

  39. ·ͱΊ
    ͳͥ Python Λ࢖͏ͷ͔
    › ൺֱతૉ௚ʹϓϩάϥϛϯάͰ͖Δ
    › ඪ४ϥΠϒϥϦ΋֎෦ϥΠϒϥϦ΋๛෋ʹ͋Δ
    › ࠔͬͨΒ SymPy Λ౰ͨΕ͹ղܾ͢Δ͜ͱ͕ଟ͍
    ఆཧͷ݁ՌΛ Python Ͱϓϩάϥϛϯάͯ͠ΈΑ͏
    › ݁ՌΛͦͷ··࣮૷ͯ͠۩ମྫΛ؍࡯͢Δ
    › ҙ֎ͳ๏ଇ͕ݟ͔ͭΔʢ͔΋ʣ
    ఆཧͷূ໌͔Β Python Ͱϓϩάϥϛϯάͯ͠ΈΑ͏
    › ߏ੒తͳূ໌͸ϓϩάϥϛϯάͰ͖Δʢ͜ͱ΋͋Δʣ
    › ϓϩάϥϛϯάͰ͖Ε͹ূ໌ΛཧղͰ͖Δʢ͔΋͠Εͳ͍ʣ
    Hayao (Hiroshima 2019) Number Theory with Python October 12, 2019 39 / 39

    View full-size slide