Bill Allombert on Sun, 27 Nov 2022 12:58:03 +0100


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

Re: A071521(n)


On Sun, Nov 27, 2022 at 11:21:14AM +0100, Karim Belabas wrote:
> Hi,
> 
>   I didn't check the definition of A071521 in OEIS but I guess the
> problem is the unexpected (if one is used to C) semantic of the 'a op= b'
> operator in PARI/GP, whose value is A = a op b (and not 'a' !). 

The following C program print 5 , not 1:

#include <stdio.h>
int main(void)
{
  long a = 1, b;
  b = a*=5;
  printf("%ld\n",b);
  return 0;
}

(all PARI/GP versions also return 5).

>   A071521(n)= my(t=1);sum(k=1,logint(n,3),logint(n\t*=3,2)+1)
> 
> when k = 1, we set t = 3 and compute logint(n \ 3, 2), where it was probably 
> expected to compute logint(n \ 1, 2). If so, a corrected version would be
> 
>   A071521bis(n) = my(t=1/3); sum(k=1,logint(n,3),logint(n\t*=3,2)+1) + 1;

Please rather use semicolon, this is perfectly legal in sum, and cleaner.

A071521bis(n) = my(t=1/3); sum(k=1,logint(n,3),t*=3;logint(n\t,2)+1) + 1;

Generaly, using '=' or 'op=' inside a function/operator argument is a bad idea,
because this disables the copy optimizer:

\g1
? A071521bis(n) = my(t=1/3); sum(k=1,logint(n,3),logint(n\t*=3,2)+1) + 1;
  ***   Warning: compiler generates copy for `n'.

Cheers,
Bill