[seqfan] Re: some gp/pari code in OEIS returns rationals, not integers

Maximilian Hasler maximilian.hasler at gmail.com
Wed Mar 11 18:40:55 CET 2009


in A004738 there's obviously an erroneous -1/2 too much in the code.

a(n)=floor(sqrt(n)+1/2)+1-abs(n-1-(floor(sqrt(n)+1/2))^2)
? vector(50,n,a(n))
time = 0 ms.
%892 = [1, 2, 1, 2, 3, 2, 1, 2, 3, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1,
2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, 2,
3, 4, 5, 6, 7, 8]


On Wed, Mar 11, 2009 at 12:46 PM, Richard Mathar
<mathar at strw.leidenuniv.nl> wrote:
>
> gg> From seqfan-bounces at list.seqfan.eu Wed Mar 11 15:54:31 2009
> gg> Date: Wed, 11 Mar 2009 16:45:15 +0200
> gg> From: Georgi Guninski <guninski at guninski.com>
> gg> To: seqfan at seqfan.eu
> gg> Subject: [seqfan]  some gp/pari code in OEIS returns rationals, not integers
> gg>
> gg> some gp/pari code in OEIS returns rationals, not integers
> gg>
> gg> A004738 - often
>
> We can repair (re-pari) this by either a PARI code that does not use floating
> point arithmetics (preferred), or an alternative one which avoids loops and
> might be quicker then if n is large:
>
>
> A004738(n)={
>        local(nred=n-1,i=1) ;
>        while(nred-2*i>=0,
>                nred -= 2*i;
>                i++ ;
>        ) ;
>        return(1+min(nred,2*i-nred)) ;
> }
>
> A004738alt(n)={
>        local(d= sqrtint(4*n-3)-1,nred) ;
>        if(d %2,
>                d--
>        ) ;
>        nred=n-1-d/2*(d/2+1) ;
>        return(1+min(nred,d+2-nred)) ;
> }
>
>
> _______________________________________________
>
> Seqfan Mailing list - http://list.seqfan.eu/
>




More information about the SeqFan mailing list