Bill Allombert on Thu, 26 Sep 2013 14:47:26 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: function result vector ? |
On Thu, Sep 26, 2013 at 01:47:41PM +0200, Christian Hoffmann wrote: > I have the following problem with a vector as a function result: > > (Trying to split a prime of form 4k+1 into two squares) For a short solution, try twosquare(p)=qfbsolve(Qfb(1,0,1),p) > 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) > } You should not put \ inside { }, and you cannot nest {. I would write 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 ) You define p two times: - local (r, p, q); ^ - forstep(p=r, ^ The second p is local to the forstep loop, so when you go out of the loop with break, p is again the first p which is still 0. You need to save the value of the second p before exiting the loop. twosquares(n) = { local (r, p, q); r=floor(sqrt(n)); forstep(p1=r,0,-1, if(issquare(n-p1^2,&q),p=p1; break)); v=[p,q]; print(p," ",q); return(v) } or even twosquares(n) = { my (r, q); r=floor(sqrt(n)); forstep(p=r,0,-1, if(issquare(n-p^2,&q),return([p,q]))); } > PS: Is there a rigorous definition of Pari syntax? See the chapter 2 of the manual. you could start by reading the rule for white spaces: 2.2.3 Special editing characters alias ??"Special editing"@2 Cheers, Bill.