Bill Allombert on Wed, 16 Sep 2026 12:07:57 +0200


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

Re: Is my understanding on how my 0-based vector access works correct?


On Tue, Sep 15, 2026 at 11:36:45PM +0200, hermann@stamm-wilbrandt.de wrote:
> In Elementary Number Theory lecture of Heidelberg University I learned a
> lot,
> lately on continued fractions and Pell equations (script behind University
> VPN).
> 
> I implemented "fundsol(d)" in pell.gp below, to determine fundamental
> solution
> of Pell equation x^2-d*y^2=1 per Corollary 6.42 of script.
> 
> I implemented 0-based vector access so that fundsol() matches the Corollary
> a_i notation
> that is 0-based for continued fractions (in proof the lecturer used -2 based
> as well sometimes).
> 
> So bottom code works, an "a(i)" is able to access cf vector 0-based.
> This worked only after quite some time and error.
> I want to get confirmation or correction on my understanding.
> 
> When cf variable gets assigned it is allocated as global variable because it
> is not in "my(...)"?
> a(i) then can access that global cf variable?

Yes, but you could also do
  my(cf=contfrac(sqrt(d)), a=i->cf[i+1]);
(this way cf is part of the context of the closure a)

and of course, you could also do
a0 = cf[1]; a = cf[2..-1]
and then use
a0, a[1], a[2], ... instead of
cf[1], cf[2], cf[3], ...

In you code, you are computing contfrac(sqrt(d)) using floating point.
? contfrac(sqrt(23))
%5 = [4,1,3,1,8,1,3,1,8,1,3,1,8,1,3,1,8,1,3,1,8,1,3,1,8,1,3,1,8,1,3,1,8,1,3,1,8,1,3,1,8,1,3,1,9]
With 2.18.1, you can compute it exactly:
? contfrac(quadgen(4*23))
%8 = [[4],[1,3,1,8]]
which means it is 4 followed by 1,3,1,8 repeating infinitely.

Cheers,
Bill.