Pythagorean triple Python

About Pythagorean triple

The Pythagorean theorem, which is the basis of the famous three-square theorem, is in a right triangle. a^2 + b^2 = c^2 Will be established.

The number of combinations in which this (a.b, c) is a natural number is called the Pythagorean triple. The genius Pythagoras, who was active in ancient BC, described this combination as the following formula:

(a,b,c) = \frac{n^2 - 1}{2}, n, \frac{n^2 + 1}{2}

I realized that I could express it as.

However, while this formula can show that there are an infinite number of Pythagorean triples, it is not exhaustive.

So is there a comprehensive formula?

The first answer is said to remain in the 7th century Indian literature.

As m> n a = k(m^2 - n^2), b = 2kmn, c = k(m^2 + n~2)

After all, Indian mathematics is advancing! !!

Output Pythagorean triple in Python

def Pythagoras(n :int) -> list:
    answer = []
    answer.append((n**2-1)/2)
    answer.append(n)
    answer.append((n**2+1)/2)
    return answer
    
for i in range(1,100,2):
    answer = Pythagoras(i)
    for i in range(0,3):
        answer[i] = int(answer[i])
    print(answer)
 
a b c
4 3 5
12 5 13
24 7 25
40 9 41
60 11 61
84 13 85
112 15 113
144 17 145
180 19 181
220 21 221
264 23 265
312 25 313

If it is a small combination, you can see that (6, 8, 10) has been skipped.


# m > n
def Pythagoras_2(k :int, m :int, n: int) -> list:
    answer = []
    answer.append(k*(m**2 - n**2))
    answer.append(2*k*m*n)
    answer.append(k*(m**2 + n**2))
    return answer

for k in range(1,5):
    for m in range(1,5):
        for n in range(1,10):
            if m <= n :
                break
            print(Pythagoras_2(k,m,n))
a b c
3 4 5
8 6 10
5 12 13
15 8 17
12 16 20
7 24 25
6 8 10
16 12 20
10 24 26
30 16 34
24 32 40
14 48 50
9 12 15
24 18 30
15 36 39
45 24 51
36 48 60
21 72 75
12 16 20
32 24 40
20 48 52
60 32 68
48 64 80
28 96 100

The combination of Pythagorean triples can be comprehensively expressed.

Recommended Posts

Pythagorean triple Python
Project Euler # 9 "Special Pythagorean Triple" in Python
Python
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Try python
Python memo
Python iterative
Python2 + word2vec
Python functions
Python tutorial
python underscore
Python summary
Start python
[Python] Sort
Note: Python
Python basics ③
python log
Python basics
[Scraping] Python scraping
Python update (2.6-> 2.7)
python memo
Python memorandum
Python # sort
ufo-> python
Python nslookup
python learning
Hannari Python 2020
[Rpmbuild] Python 3.7.3.
Prorate Python (1)
python memorandum
Download python
python memorandum
Python memo