1

I want to implement a fast algorithm (avoiding or at least minimizing bruteforce) which for a given square number $n$ calculates a series of positive integers $y$ up to a certain limit $l$ such that $n+y^2$ is again a perfect square?

I can obtain the trivial case $y=\frac{1}{2}(n-1)$ as described in the answer and comments on this question using the equation $(x-y)(x+y)=x^2-y^2$.

Example: Let us take $n=225(=15^2)$, we obtain one solution $y=\frac{1}{2}(225-1)=112$ directly. But how we can obtain the whole list $(8, 20, 36, 112)$?

My ideas: As described here I came across the so called "Norm-Form Equation", which is a diophantine equation of the form $x^2-Dy^2=n$ and we have the special case $D=1$. To deal with this Norm-Form Equation, in his book Richard A. Mollin involves the divisor function.

Refering to our example, if we can deduce the other $y$-values $8,20,36$ from the (directly obtained) value $y=112$, it would allow me to speed up my bruteforce search drastically.

Note: If we cannot avoid bruteforce completely, I would be greateful for an approach that at least minimizes the search for these $y$-values.

  • 1
    If you have the divisors of $m$ it's easy to generate all the Pythagorean triples via the standard parameterization of the primitive triples: $$(u^2-v^2)^2+(2uv)^2=(u^2+v^2)^2$$ – PM 2Ring Jan 27 '22 at 09:45
  • Good hint - thank you. So it requires us to factorize $n$ (which is not a very efficient operation, but which is doable). – Eldar Sultanow Jan 27 '22 at 09:47
  • 2
    This is called generalized Pell equation and there is an algorithm for finding all solutions, but it is complicated. Wolfram gives references to Beiler and Dickson. Some solutions can be generated from a single one, but not all. – Conifold Jan 27 '22 at 10:02

3 Answers3

4

For the given problem, you neither need Pell-like equations nor Pythagorean triples. You just need , as mentioned , the divisors of $\ n\ $ :

You want the solutions of $$n+y^2=z^2$$ with positive integers $\ y\ $ and $\ z\ $

This means $$n=(z-y)(z+y)$$

Hence every pair of positive integers $a,b$ with $ab=n$ , where $a,b$ have the same parity gives a solution : $y=\frac{a-b}{2}$ , $z=\frac{a+b}{2}$

To ensure that $y$ is positive, $a$ must exceed $\sqrt{n}$

In PARI/GP, the following routine determines the pairs of positive integer solutions :

gp > n=225;fordiv(n,s,t=n/s;if(s^2>n,if(Mod(s,2)==Mod(t,2),[y,z]=[(s-t)/2,(s+t)/2];print(y," ",z))))
8 17
20 25
36 39
112 113
gp >
Peter
  • 84,454
  • 1
    BTW, although it doesn't matter for correctness of mathematical explanation, original question had letter n instead of m. Also OP used x instead of your z in his Question. But corretness of Answer is alright without this. – Arty Jan 27 '22 at 11:30
  • 1
    I fixed the issue with "$m$" because I explicitely mentioned $\sqrt{n}$ – Peter Jan 27 '22 at 11:40
  • @Peter: Thank you for this helpful algorithm. It speeds up searching routines. Arty: We just need to transfer Peter's PARI/GP Code into Python. – Eldar Sultanow Jan 27 '22 at 12:00
  • 1
    @EldarSultanow Please create Stack-Overflow Question with request for Python version. I already wrote a big Answer, with advanced Python code. But waiting for you to ask a Question. I did that answer and code few hours before Peter wrote his Answer. So my ideas and code is a bit different, but uses same common sub-idea that we just need to use Factorization as a fast way of finding all (X, Y) pairs. – Arty Jan 27 '22 at 12:19
  • 2
  • @PM2Ring Nice and simple Python version, thanks! – Arty Jan 27 '22 at 15:31
2

We begin with a formula that will generate $\space (n,y,z):(n+y^2=z^2\space$ given two parameters which can be found given any number in the triple.

$$ n=(m^2-k^2)^2\qquad y=2mk\qquad z=m^2+k^2$$ We solve the $\space n$-function for $\space k,\space$ in terms of $\space m \space$ and $\space n.\quad$ Then we test a defined range of $\space m$-values and, any that yield an integer $\space k\space$ indicate the $\space (m,k)\space$ values needed to generate a triple.

$$n=(m^2-k^2)^2\implies k=\sqrt{m^2-\sqrt{n}}\\ \text{for}\qquad \sqrt{\sqrt{n}+1} \le m \le \frac{\sqrt{n}+1}{2}$$ The lower limit ensures $k\in\mathbb{N}$ and the upper limit ensures $m> k$. $$n=15^2\implies \sqrt{15+1}=4\le m \le \frac{15+1}{2} =8\\\text{and we find}\quad m\in\{4,8\}\implies k \in\{1,7\} $$ $$F(4,1)=(225,8,17)\qquad F(8,7)=(225,112,113) $$ But what about the other two $\space y$-values? We find a triple for each of the prime factors of $\space \sqrt{n}\space$ and then multiply the results by the cofactor(s). $\quad 225=3^2\times5^2\space$ so $$n=3^2\implies \sqrt{3+1}=2\le m \le \frac{3+1}{2} =2\\ \text{and we find}\quad m\in\{2\}\implies k \in\{1\} $$ $$F(2,1)=(3^2,4,5)\longrightarrow 5^2(3^2,4,5)=(225,20,25) $$

$$n=5^2\implies \sqrt{5+1}=3\le m \le \frac{5+1}{2} =3\\ \text{and we find}\quad m\in\{3\}\implies k \in\{2\} $$ $$F(3,2)=(5^2,12,13)\longrightarrow 3^2(5^2,12,13)=(225,36,39) $$

poetasis
  • 6,338
  • Great approach - will try this too! – Eldar Sultanow Feb 19 '22 at 08:55
  • @poetasis Maybe you can provide also a PseudoCode in any language (even english + math), a function that is given n and limit l, produces all integer solutions (x, y) of equation n + y^2 = x^2. This is exactly what is needed for our project. We will convert your PseudoCode to Python or C++ in our project. Also your formulas work only if factorization of n is known, or they can work even without factorization? – Arty Feb 19 '22 at 10:29
  • @Arty I hope my second answer is useful to you. The limit (l) is computed automatically. – poetasis Feb 19 '22 at 15:30
2

Suggested code too big for a comment

Here is a test run of the code that follows it.

 Enter squared number n to find Y,Z.
 ? (3*5*7)^2
 Start time     09:06:11
 Input was 11025 
 F(2 , 1 )=(9 , 4 , 5 ) f2 = 35 ––> (11025 , 140 , 175 )

F(3 , 2 )=(25 , 12 , 13 ) f2 = 21 ––> (11025 , 252 , 273 )

F(4 , 3 )=(49 , 24 , 25 ) f2 = 15 ––> (11025 , 360 , 375 )

F(4 , 1 )=(225 , 8 , 17 ) f2 = 7 ––> (11025 , 56 , 119 ) F(8 , 7 )=(225 , 112 , 113 ) f2 = 7 ––> (11025 , 784 , 791 )

F(5 , 2 )=(441 , 20 , 29 ) f2 = 5 ––> (11025 , 100 , 145 ) F(11 , 10 )=(441 , 220 , 221 ) f2 = 5 ––> (11025 , 1100 , 1105 )

F(6 , 1 )=(1225 , 12 , 37 ) f2 = 3 ––> (11025 , 36 , 111 ) F(18 , 17 )=(1225 , 612 , 613 ) f2 = 3 ––> (11025 , 1836 , 1839 )

F(11 , 4 )=(11025 , 88 , 137 ) f2 = 1 F(13 , 8 )=(11025 , 208 , 233 ) f2 = 1 F(19 , 16 )=(11025 , 608 , 617 ) f2 = 1 F(53 , 52 )=(11025 , 5512 , 5513 ) f2 = 1 Stop time 09:06:11

This is written in BASIC because it is easier than Python and available anywhere. Note: For those who don't speak BASIC, a colon (:) permits another command on the same line, a comma (,) prints a tab character, and a semicolon (;) does not print a carriage return so the next print statement will continue on the same line.

Note: $\space f1\space $ and $\space f2\space $ are the cofactors of $\space \sqrt{n}.$

 110 print : print
 120 print "Enter squared number n to find Y,Z."
 130 input n1
 140 print "Start time ",time$
 150 print "Input was " n1
 160 x1 = sqr(n1)
 170 if x1 = int(x1)
 180   for f1 = 1 to x1
 190     f2 = x1/f1
 200     if f2 = int(f2)
 210       gosub 290
 220     endif
 230   next f1
 240   print "Stop time ",time$
 250   end
 260 else 
 270   print "The number " n1 "is not a square.  "
 280   goto 120
 290 print
 300   v1 = int(sqr(f1+1)) : v9 = int((f1+2)/2)
 310   for m1 = v1 to v9
 320     if m1 > 1
 330       k1 = sqr(m1^2-f1)
 340       if k1 = int(k1)
 350         y1 = 2*m1*k1
 360         z1 = m1^2+k1^2
 370         print "F(" m1 ", " k1 ")=(";
 380         print f1^2 ", " y1 ", " z1 ") f2 = " f2;
 390 if f2 > 1
 400   print "––> ";
 410   print "(" f1^2*f2^2 ", " y1*f2 ", " z1*f2 ")"
 420 else 
 430   print
 440 endif
 450       endif
 460     endif
 470   next m1
 480 return
poetasis
  • 6,338
  • 1
    Amazing - Thanks! With Arty we will try to convert this ino python try this out. By the way we figured out some interesting behavior: https://math.stackexchange.com/questions/4386006/why-do-we-have-these-occurrences-of-primes-of-the-form-4k1-in-square-differen – Eldar Sultanow Feb 19 '22 at 15:32
  • Thanks for great code! Up-Voted. Reading it to understand and convert to our C++ project. – Arty Feb 19 '22 at 15:38