I'm trying to compute the following sum in PARI/GP $C=\sum_{n=1}^{\infty} \frac{g(n)}{n^2}$ where $g(n)$ defined as $$g(n)=(-1)^r, \qquad r=\text{number of even indexed prime factors of $n$}$$ By even indexed primes I mean the primes 3, 7, 13, 19, 29... corresponding to $p_2, p_4, p_6, p_8, p_{10} \ldots$ where $p_n$ is the $n$th prime number. @YiyuanLee suggested the following pari code to compute $g(n)$
compute(n) =
{
g = 1;
index = 0;
p = 0;
while(p < n,
p = nextprime(p + 1);
index += 1;
while(n % p == 0, n = n / p; if (index % 2 == 0, g *= -1));
);
printf("%d\n", g);
}
Although it gives individual values of $g(n)$ nicely, I cannot seem to use it to compute $C$. I tried the following in PARI/GP,
sum(n=1, N, compute(n)/n^2)
But all I get is a list of values of $g(n)$ all the way till the value for $N$ and a final output of $0$. So how do I ask PARI to do the sum.
Your procedure 'compute' must first become a function so replace
printf("%d\n", g);byg(the function's result orreturn g)Your sum is in fact a second function of N so define :
After that try for example
If you want real values multiply compute(n) by 1.0. and try (as indicated by Derek) suminf or rather sumalt (with more precision asked by \p) :
Increasing the precision will give you more significative digits! The result is near $1.230403$.
Here it seems easier to compute directly some values :
(pari/gp tutorial should help you to learn more)