LoÃc Grenià on Thu, 26 Sep 2013 14:14:01 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: function result vector ?


2013/9/26 Christian Hoffmann <c-w.hoffmann@sunrise.ch>:
> I have the following problem with a vector as a function result:
>
> (Trying to split a prime of form 4k+1 into two squares)
>
> twosquares(n) = \
>  local (r, p, q);\
> {\
> r=floor(sqrt(n));\
> v=vector(2);\
> v=[-1,0];\
> forstep(p=r,0,-1, if(issquare(n-p^2,&q),{v=[p,q]; print(v); break}));\
> \\forstep(p=r,0,-1, if(issquare(n-p^2,&q), break;));\
> v=[p,q];\
> print(p."  ",q)
> return(v)
> }
>
> twosquares(17)
> 0   1
> %50 = [0, 1]
>
> instead of [4, 1] . ( 17 = 4^2 + 1^2 )

    First of all with which version of pari/gp are you using this script ?
  It does not work with any recent version (embedded braces are
  forbidden). Moreover the backslashes inside the braces are
  useless.

    Inside the loop the variable p is local to the loop and does not
  modify the external p. Your script is equivalent to

twosquares(n) =
{
 local (r, p, q);
  r=floor(sqrt(n));
  v=[-1,0];
  forstep(p2=r,0,-1, ifissquare(n-p2^2,&q),{v=[p2,q]; print(v); break));
  v=[p,q];
  print(p,"  ",q);
  return(v)
}

  It is clearer that the p you are using is not the one you thought.

> Christian
>
> PS: Is there a rigorous definition of Pari syntax?

    Yes, in the documentation, more precisely in chapter 2 of the
  users manual. You moreover have a tutorial to get you started.