Present the even number (if it divides by 4) as a difference of two integer squares ?

Max Alekseyev maxale at gmail.com
Thu Apr 24 20:33:54 CEST 2008


Richard,

I think the following PARI code is more effective (and shorter).
I am addressing the case of multiples of 4 but it is clear how to
extend this to odd numbers if needed.

{ printa2b2(n) = if(n%4!=0,error("not a multiple of 4"));
fordiv(n\4,d, if(4*d^2>=n,break); print(n\4\d+d,"^2 - ",n\4\d-d,"^2"))
}

Sample execution:

? printa2b2(1000)
251^2 - 249^2
127^2 - 123^2
55^2 - 45^2
35^2 - 15^2

Regards,
Max

On Thu, Apr 24, 2008 at 9:02 AM, Richard Mathar
<mathar at strw.leidenuniv.nl> wrote:

>  ap> Does anybody has a good PARI program to present the given even
>  ap> positive integer (if it divides by 4^k, k >= 1 ) as a difference of
>  ap> two positive integer squares (desirably generating all "k" variants)
>  ap> ?
>
>  I think that searching for the pythagorean triangle side lengths could
>  reveal a PARI implementation. The one below would decompose an integer n
>  into all possible n=a^2-b^2 (including positive and negative b) and print them:
>
>
>  /* Print parameters 0<=b<=a such that n=a^2-b^2.
>   @parameter n the number to be decomposed
>  */
>  decoSq(n)={
>         local(a,b,dvs) ;
>         /* n=(a+b)(a-b); search thru a+b as a divisor of n
>         */
>         dvs=divisors(n) ;
>         for(i=1,length(dvs),
>                 /* a+b = dvs[i], a-b=n/dvs[i]; subtract both equations for b
>                 */
>                 b= (dvs[i]-n/dvs[i])/2 ;
>                 if( type(b)=="t_INT",
>                         a=dvs[i]-b ;
>                         print(n," = ",a,"^2 - ",b,"^2") ;
>                 ) ;
>                 b= (n/dvs[i]-dvs[i])/2 ;
>                 if( type(b)=="t_INT",
>                         a=-dvs[i]-b ;
>                         print(n," = ",a,"^2 - ",b,"^2") ;
>                 ) ;
>         ) ;
>  }
>
>  {
>         /* example call */
>  decoSq(300) ;
>  }
>





More information about the SeqFan mailing list