1

Pythagorean triples where $A-B=\pm1$ are some of the rarest; the $19^{th}$ has terms $A,B,C$ in the quadrillions. I found a formula in a book, "Pythagorean Triangles" that generates them in sequence beginning with a seed triple $T_1=(3,4,5)$: $A=3A+2C+1\quad B=3A+2C+2\quad C=4A+3C+2$ will generate $T_2=(20,21,29)\quad T_3=(119,120,169)\quad T_4=(697,696,985)$ and so on. Nineteen iterations gives you the first $19$ triples and that is great but I developed a formula which uses less computation until you get to the $n^{th}$ triple you want to view. It generates the parameters $(m,n)$ to feed Euclid's formula:$$A=m^2-n^2\quad B=2mn\quad C=m^2+n^2$$

The formula is $\quad m_{x+1}=2m_x+n_x\quad n_{x+1}=m_x\quad $ and generates the following pairs with a seed: $P_0=(1,0)$. $$P_1=(2,1)\quad P_2=(5,2)\quad P_3=(12,5)\quad P_4=(29,12)\quad P_5=(70,29)\quad P_6=(169,70)\quad ...$$

I would like to be able to generate the $6^{th}$ or the $1000^{th}$ pair directly without generating $1$-thru-$5$ or $1$-thru-$999$ to get there but I haven't been able to figure out any way to generate an individual pair directly. I have tried $2=2^1, 5=2^2+2^1, 12=2^3+2^2, hmm, 29=2^4+2^3+2^2+2^1+2^0 $ and other things like factors of $2,5,12,29...$ and I'm out of ideas.

Is it possible to generate an $x^{th}$ member pair using just $x$ as an input number or, by the nature of this sequence, is it required to generate all of them in order until I get to the desired pair?

Someone said my formula does not work but here it is working in a spreadsheet.

enter image description here

poetasis
  • 6,338
  • Your own work does not generate the $T_n$, and $T_0$ is wrong. – Parcly Taxel Oct 18 '19 at 16:55
  • Pythagorean triples of the form $(a,a+1,c)$ are rare because the $a$-value of the $n$-th smallest of such triple grows exponentially, i.e., as fast as $(1+\sqrt{2})^n$. Amongst Pythagorean triples $(a,b,c)$ with $0\leq a\leq b\leq m$, you can expect only around $\dfrac{\ln(m)}{\ln(1+\sqrt{2})}$ triples of the form $(a,a+ 1,c)$. – Batominovski Oct 18 '19 at 17:13
  • 1
    Here you may find an algorithm for the efficient computation of Fibonacci/Lucas numbers by repeat squaring: https://math.stackexchange.com/a/2081266/44121. In your case you have to generate Pell numbers, but the principle is pretty much the same: in order to compute $(P_{2m},P_{2m+1})$ you only need $(P_m,P_{m+1})$. – Jack D'Aurizio Oct 18 '19 at 17:41
  • @ Jack D'Aurizio The equation from the Pells equation page: $$P_n=\frac{(1+\sqrt{2})^n-(1-\sqrt{2})^n}{2\sqrt{2}}$$ is perfect for my needs. – poetasis Oct 18 '19 at 18:46

2 Answers2

2

The computation of the Pythagorean triples of the form $T_n=(a_n,a_n+1,c_n)$ is equivalent to the computation of some convergents of the continued fraction of $\sqrt{2}$: in particular $$ [1;\underbrace{2,2,\ldots,2,2}_{2n\text{ times}}]=\frac{2a_n+1}{c_n} $$ where $$ c_n = \frac{(1+\sqrt{2})^{2n+1}-(1-\sqrt{2})^{2n+1}}{2\sqrt{2}},\qquad 2a_n+1 =\frac{(1+\sqrt{2})^{2n+1}+(1-\sqrt{2})^{2n+1}}{2}=d_n $$ both fulfill the recurrence $\ell_{n+2}=6\ell_{n+1}-\ell_n$. They can be expressed in terms of $D_n$ and $D_{n+1}$, where $$ D_n = (3+2\sqrt{2})^n+(3-2\sqrt{2})^n =\sigma^n+{\bar{\sigma}}^n$$ is the trace of the $n$-th power of a $2\times 2$ matrix. This sequence fulfills

$$ D_{2n} = D_n^2-2,\qquad D_{2n+1}=D_n D_{n+1}-6 \tag{R}$$ so the couple $(D_n,D_{n+1})$ can be computed by a repeat-squaring algorithm. A concrete example will hopefully clarify how. Let us assume to want to compute $D_{23}$ and $D_{24}$. The binary representation of $23$ is $10111_2$, so we compute the couples $(D_m,D_{m+1})$ for $m=1_2,10_2,101_2,1011_2$ and finally $10111_2$ via $(R)$. $$ (D_1,D_2)=(6,34) $$ $$ (D_2,D_3)= (34,198)$$ $$ (D_5,D_6)=(6726,39202) $$ $$ (D_{11},D_{12})=(263672646,15367968024) $$ $$ (D_{23},D_{24})=(405211279147678086,2361744410637427202)$$ This gives us $c_{23}$ and $d_{23}$, thus $T_{23}$, with no more than $3\log_2(23)$ multiplications.

Jack D'Aurizio
  • 353,855
  • The equation in my comment to your comment above is all I need. I put it in a spreadsheet and the numbers out are perfect. Your answer showed a variation of it so I marked yours as correct. I don't follow the rest but the rest doesn't seem to matter. Now I can embed this equation somewhere and generate the $1000{th}$ parameter pair or Pythagorean triple in one step. Thank you. – poetasis Oct 18 '19 at 18:55
-1

Edited answer...

which starts with a comment.

The title Looking for the best way to find Pythagorean triples where $B−A=\pm1$ is possibly misleading for the actual question, which after many comments can be extracted only from the fact that the initial answer for the $(A,C)$ recursion (remained in the sequel) is not the wanted answer. Please always make clear in the OP which is the question (without alternatives, best stated as question, not as a wish).

The OP gives two ways to construct Pythagorean triples, one is "from a book" and constructs triples $T_x=(A_x,B_x,C_x)$ recursively, the other one comes from a formula, it constructs intermediate pairs $P_x=(m_x,n_x)$ given by an other recursion, that may be used to reconstruct $(A_x,B_x,C_x)$. As i extract the information from the comments, we need only the $P_x$ (despite of the title, and the initial digression / divagation on triples). OK, this is even simpler.


We have the linear recursion formula for the pair written as column vector $$ \begin{aligned} \begin{bmatrix} m_{x+1}\\ n_{x+1} \end{bmatrix} &= \begin{bmatrix} 2&1\\1&0 \end{bmatrix} \begin{bmatrix} m_{x}\\ n_{x} \end{bmatrix} \\ &= \underbrace{ \begin{bmatrix} 1+\sqrt 2&1-\sqrt 2\\1&1 \end{bmatrix}}_C \underbrace{ \begin{bmatrix} 1+\sqrt 2&\\&1-\sqrt 2 \end{bmatrix}}_D \underbrace{ \frac 1{2\sqrt 2} \begin{bmatrix} 1 &\sqrt 2-1\\-1&\sqrt 2+1 \end{bmatrix}}_{C^{-1}} \begin{bmatrix} m_{x}\\ n_{x} \end{bmatrix} \ , \end{aligned} $$ with a diagonal matrix $D$ above. The powers of $\begin{bmatrix}2&1\\1&0\end{bmatrix}$ are not immediate, but the powers of $D$ are. We get directly for your pairs formula the explicit version with proof: $$ \begin{aligned} \begin{bmatrix} m_x\\n_x \end{bmatrix} &= (CDC^{-1})^x\begin{bmatrix}m_0\\n_0\end{bmatrix} = CD^xC^{-1}\begin{bmatrix}1\\0\end{bmatrix} \\[2mm] &= \frac 1{2\sqrt 2} \begin{bmatrix} 1+\sqrt 2&1-\sqrt 2\\1&1 \end{bmatrix} \begin{bmatrix} (1+\sqrt 2)^x&\\&(1-\sqrt 2)^x \end{bmatrix} \begin{bmatrix} 1 &\sqrt 2-1\\-1&\sqrt 2+1 \end{bmatrix} \begin{bmatrix}1\\0 \end{bmatrix} \\[2mm] &= \frac 1{2\sqrt 2} \begin{bmatrix} (1+\sqrt 2)^{x+1} & (1-\sqrt 2)^{x+1}\\ (1+\sqrt 2)^{x } & (1-\sqrt 2)^{x } \end{bmatrix} \begin{bmatrix} 1 &\sqrt 2-1\\-1&\sqrt 2+1 \end{bmatrix} \begin{bmatrix}1\\0 \end{bmatrix} \\[2mm] &= \frac 1{2\sqrt 2} \begin{bmatrix} (1+\sqrt 2)^{x+1} - (1-\sqrt 2)^{x+1} & ? \\ (1+\sqrt 2)^{x } - (1-\sqrt 2)^{x } & ? \end{bmatrix} \begin{bmatrix}1\\0 \end{bmatrix} \\[2mm] &= \frac 1{2\sqrt 2} \begin{bmatrix} (1+\sqrt 2)^{x+1} - (1-\sqrt 2)^{x+1} \\ (1+\sqrt 2)^{x } - (1-\sqrt 2)^{x } \end{bmatrix} \ , \end{aligned} $$ which gives $\displaystyle n_x=\frac 1{2\sqrt 2}\Big(\ (1+\sqrt 2)^{x } - (1-\sqrt 2)^{x }\ \Big)$.

$\square$

In particular, for $x>0$ we can compute $n_x$ by rounding to an integer the number $\displaystyle n_x=\frac 1{2\sqrt 2} (1+\sqrt 2)^x$. For instance, for $x=7$ we obtain $168.99926035179\dots$ and after rounding we get $n_7=169$.


Older answer for the explicit form of the $(A,C)$ recursion:

You may try to write explicitly the recursion formula as passing from one pair $(A,C)$ to the next pair $(A',C')$ in the form: $$ \begin{aligned} \begin{bmatrix} A'\\C'\\1 \end{bmatrix} &= \underbrace{ \begin{bmatrix} 3&2&1\\4&3&2\\0&0&1 \end{bmatrix}}_{=:T} \begin{bmatrix} A\\C\\1 \end{bmatrix} \ , \\ &\qquad\text{ extracted from the mentioned passage} \\ A' &=3A+2C+1\ ,\\ C' &=4A+3C+2\ , \end{aligned} $$ thus introducing a matrix $T$. Its charactristic polynomial is $(x - 1) (x^2 - 6x + 1)$, so we have the following diagonalization over $\Bbb Q(a)$ with $a=\sqrt 2$. We expect a diagonal matrix with diagonal entries corresponding to the roots of the above characteristic polynomial, they are $1$ and $3\pm 2\sqrt 2$. Computer aided typing:

sage: K.<a> = QuadraticField(2)
sage: a^2
2
sage: T = matrix( K, 3,3, [3,2,1, 4,3,2, 0,0,1] )
sage: T
[3 2 1]
[4 3 2]
[0 0 1]
sage: D, change = T.jordan_form(transformation=True)
sage: D
[ 2*a + 3|       0|       0]
[--------+--------+--------]
[       0|       1|       0]
[--------+--------+--------]
[       0|       0|-2*a + 3]
sage: change
[ 1  1  1]
[ a  0 -a]
[ 0 -2  0]
sage: change * D * change^-1 == T
True

Later edit: Let us use, and type explicitly the obtained formula for $T$, and thus also for the powers of $T$. Below, $S$ is the base change matrix. $$ \begin{aligned} T &= \underbrace{ \begin{bmatrix} 1&1&1\\ a&0&-a\\ 0&-2&0 \end{bmatrix}}_S \begin{bmatrix} 3+2a&&\\ &1&\\ &&3-2a \end{bmatrix} \underbrace{ \frac 14 \begin{bmatrix} 2 & a & 1\\ 0&0&-2\\ 2&-a&1 \end{bmatrix}}_{S^{-1}} \\ T^n &= \frac 14 \begin{bmatrix} 1&1&1\\ a&0&-a\\ 0&-2&0 \end{bmatrix} \begin{bmatrix} (3+2a)^n&&\\ &1&\\ &&(3-2a)^n \end{bmatrix} \begin{bmatrix} 2 & a & 1\\ 0&0&-2\\ 2&-a&1 \end{bmatrix} \\[3mm] &\qquad\text{ Above }a=\sqrt 2\ . \\ &\qquad\text{ This gives an }\color{blue}{\text{explicit formula}:} \\[3mm] \color{blue}{ \begin{bmatrix} A_n\\ C_n\\ 1 \end{bmatrix}} &= T^n \begin{bmatrix} A_0\\ C_0\\ 1 \end{bmatrix} = T^n \begin{bmatrix} 0\\ 1\\ 1 \end{bmatrix} \\ &= \color{blue}{ \frac 14 \begin{bmatrix} 1&1&1\\ a&0&-a\\ 0&-2&0 \end{bmatrix} \begin{bmatrix} (3+2a)^n&&\\ &1&\\ &&(3-2a)^n \end{bmatrix} \begin{bmatrix} 2 & a & 1\\ 0&0&-2\\ 2&-a&1 \end{bmatrix} \begin{bmatrix} 0\\ 1\\ 1 \end{bmatrix}}\ . \end{aligned} $$

$\square$


Please stop reading here if computer experiments are not welcome. (I posted this since the shape of the OP also has similar collection of data. It is often for me a check, and in this case it may be helpful for some readers, too.)


Example of an explicit computation:

To compute -say- the $15$.th term (or so) we already have $D^{15}$, so compute with sage the product: $$ \frac 14 \begin{bmatrix} 1&1&1\\ a&0&-a\\ 0&-2&0 \end{bmatrix} \begin{bmatrix} (3+2a)^{15}&&\\ &1&\\ &&(3-2a)^{15} \end{bmatrix} \begin{bmatrix} 2 & a & 1\\ 0&0&-2\\ 2&-a&1 \end{bmatrix} \begin{bmatrix} 0\\1\\1 \end{bmatrix} \ , $$ and we get:

sage: Z = matrix(3, 1, [0,1,1])    # initial solution, A=0, C=1
sage: change * D^15 * change^-1 * Z
[183648021599]
[259717522849]
[           1]
sage: A15, C15 = 183648021599, 259717522849
sage: B15 = A15 + 1
sage: A15^2 + B15^2 == C15^2
True

This is the kind of direct computation needed. (The diagonal matrix $D$ has diagonal entries $1$, and $3\pm 2\sqrt 2$, so the only involved involved computation is the one of the powers of these diagonal entries.)

dan_fulea
  • 32,856
  • What I want to avoid is recursion and I have no idea what language you are using in your greyed examples. Pell numbers provided the answer I needed and a single equation gives me any Pell number I choose. – poetasis Oct 18 '19 at 19:00
  • The present answer was designed to answer the question: Is it possible to generate an xth member pair using just x as an input number or, by the nature of this sequence, is it required to generate all of them in order until I get to the desired pair? The answer described above is as follows. Consider the diagonal matrix $D$ in my answer. I assume you can compute the powers of $3+2a=(3+2\sqrt 2)$. (Numerically may also be ok, since the final result will be an integer.) For $x=15$ compute that one multiplication of four matrices, which is explicitly displayed. No recursion. – dan_fulea Oct 18 '19 at 22:27
  • @poetasis The programming language used is sage, www.sagemath.org . It is a mathematically structured piece of software, often used in mathematical computations. But if this is annoying, then just accept the typed Jordan form decomposition of the matrix $T$, explicitly $$T=SDS^{-1}$$ with an explicit base change matrix $S$. Using only this simple linear algebra mechanism, we get the passage from one pair $(A_x,C_x,1)$ to the next one. The Jordan form lives over the field $\Bbb Q(\sqrt 2)$. I am using the short notation $a$ for $\sqrt 2$. What is now still unclear, please? – dan_fulea Oct 18 '19 at 22:33
  • I am an amateur, 40 years removed from academia. I do a lot with computers but I know nothing about the sage language and I do not understand the math you presented. If it has to do with the formula I found in a book, it does not address my question about $my$ formula, which I think is better. The Pell equation solved my problem. Your answer just puzzled me because the things you talked about are outside my experience... the Jordan form? I hope your answer may help others more learned than I. – poetasis Oct 19 '19 at 11:10
  • @poetasis The 40 years are a lot, but still, matrix multiplication should be no problem for them, this is basic. Please not that the above comes with a proof of a formula that quickly gives, as requested the $6$.th or the $1000$.th term. The question may have changed from the moment i answered it, it was a quick answer. As it came, there was nothing about "Pell equation" and "Pell numbers" around. (There still isn't in the OP.) The process of diagonalization is natural in such cases, just take a look at linear recurrences. Here is a link... – dan_fulea Oct 20 '19 at 04:48
  • ... https://en.wikipedia.org/wiki/Recurrence_relation#Solving_homogeneous_linear_recurrence_relations_with_constant_coefficients (The Pell equation is also mentioned there.) Even if this is too advanced, there is a simple check of the fact, that above we have a valid formula. Please mention which is your formula, the one which is better. – dan_fulea Oct 20 '19 at 04:51
  • For the computer aided part, i tried to rewrite the answer, so that it became only an auxiliary tool, which can (and should in your case) be completely ignored. Now i am addressing the comment about sage, a language that you never heard. Well, i am quite sure that you heard about python, it is the programming language which is now in the the top for decent software development. William Stein made a wonderful job by joining together all maths software existent on the free (or non-free) market, like PARI, GAP, Singular, Maxima, NTL, GMP, (mathematica, magma, ...), all these is sage or sagemath. – dan_fulea Oct 20 '19 at 04:57
  • @ dan_fulea I've heard of Python but never used it. Most of my needs are met by C or even BASIC. Pell numbers were recommended by Jack D'Aurizio in his first comment to my question. They served my needs and nothing else was useful. – poetasis Oct 20 '19 at 07:33
  • My formula is the one where $m_{x+1}=m_x+n_x\quad n_{x+1}=m_x$. – poetasis Oct 20 '19 at 07:40