%mathpiper,def="RamanujanSum"

/* Implementation of some number theoretical functions for MathPiper */
/* (C) 2002 Pablo De Napoli <pdenapo@yahoo.com> under GNU GPL */

/* RamanujanSum(k,n) = the sum of the n-th powers of the
k-th primitive roots of the identity */

10 # RamanujanSum(k_IsPositiveInteger,0) <-- Totient(k);

20 # RamanujanSum(k_IsPositiveInteger,n_IsPositiveInteger) <--
[
 Local(s,gcd,d);
 s:= 0;
 gcd := Gcd(n,k);
 ForEach (d,DivisorsList(gcd))
  s:=s+d*Moebius(k/d);
 s;
];

%/mathpiper



%mathpiper_docs,name="RamanujanSum",categories="User Functions;Number Theory"
*CMD RamanujanSum --- compute the "Ramanujan sum"
*STD
*CALL
	RamanujanSum(k,n)

*PARMS

{k}, {n} -- positive integers

*DESC
This function computes the Ramanujan sum, i.e. the sum of the $n$-th powers of 
the $k$-th primitive roots of the unit:

$$ Sum(l,1,k, Exp(2*Pi*I*(l*n)/k)) $$

where $l$ runs thought the integers between $1$ and $k-1$ that are coprime to $l$.

The computation is done by using the formula in T. M. Apostol,
<i>Introduction to Analytic Theory</i> (Springer-Verlag), Theorem 8.6.
%/mathpiper_docs