login
Alternating bit sum (A065359) for n-th prime p: replace 2^k with (-1)^k in binary expansion of p.
2

%I #22 Jul 20 2024 12:50:07

%S -1,0,2,1,-1,1,2,1,2,2,1,1,-1,-2,-1,2,-1,1,1,2,1,1,2,2,1,2,1,-1,1,2,1,

%T -1,-1,-2,2,1,1,-2,-1,-1,-1,1,-1,1,2,1,1,1,-1,1,-1,-1,1,-1,2,2,2,1,4,

%U 2,1,2,1,2,1,2,1,4,2,4,2,2,1,4,1,2,2,1,2,1,-1,1,-1,1,1,-1,2,1,2,1,2,2,1,-1,1,2,2,-1,-2,1

%N Alternating bit sum (A065359) for n-th prime p: replace 2^k with (-1)^k in binary expansion of p.

%C Only 3d = 11b has an alternating sum of 0.

%H Harry J. Smith, <a href="/A065081/b065081.txt">Table of n, a(n) for n=1..1000</a>

%H William Paulsen, wpaulsen(AT)csm.astate.edu, <a href="http://www.csm.astate.edu/~wpaulsen/primemaze/mazepart.html">Partitioning the [prime] maze</a>

%e The sixth prime is 13d = 1101b -> -(1)+(1)-(0)+(1) = 1 = a(6)

%t f[n_] := (d = Reverse[ IntegerDigits[n, 2]]; l = Length[d]; s = 0; k = 1; While[k < l + 1, s = s - (-1)^k*d[[k]]; k++ ]; s); Table[ Prime[ f[n]], {n, 1, 100} ]

%o (PARI)

%o baseE(x, b)=

%o {

%o local(d, e=0, f=1);

%o while (x>0, d=x-b*(x\b); x\=b; e+=d*f; f*=10);

%o return(e)

%o }

%o SumAD(x)=

%o {

%o local(a=1, s=0);

%o while (x>9, s+=a*(x-10*(x\10)); x\=10; a=-a);

%o return(s + a*x)

%o }

%o { for (n=1, 1000, p=prime(n);

%o s=SumAD(baseE(p, 2)); write("b065081.txt", n, " ", s) )

%o } \\ _Harry J. Smith_, Oct 06 2009

%o (PARI)

%o f(p)=

%o {

%o v=binary(p);

%o L=#v; u=1; s=0;

%o forstep(k=L,1,-1, if(v[k]==1,s+=u); u=-u;);

%o return(s)

%o };

%o for(n=1,100,p=prime(n); an=f(p);print1(an,", ")) \\ _Washington Bomfim_, Jan 16 2011

%o (Python)

%o from sympy.ntheory import digits, prime

%o def A065081(n): return sum((0,1,-1,0)[i] for i in digits(prime(n),4)[1:]) # _Chai Wah Wu_, Jul 19 2024

%Y Cf. A065359.

%K base,easy,sign

%O 1,3

%A _Robert G. Wilson v_, Nov 09 2001