# How can I implement Jordan's totient function?

How can I implement Jordan's totient function? It is a generalization of Euler's Phi function.

• Well, you have the definition on hand. What have you tried so far to implement it? – MarcoB Jun 2 '16 at 16:29
• See Totient Function you can download an file here – user9660 Jun 2 '16 at 18:51

Mathematica code given by Enrique Pérez Herrero at OEIS A007434

Clear[jordanTotient]

jordanTotient[n_, k_: 1] :=
DivisorSum[n, #^k*MoebiusMu[n/#] &] /; (n > 0) && IntegerQ[n];


This could also be written as

Clear[jordanTotient]

jordanTotient[n_Integer?Positive, k_: 1] :=
DivisorSum[n, #^k*MoebiusMu[n/#] &];


For k=1 this is Euler totient function

And @@ (jordanTotient[#] == EulerPhi[#] & /@ Range[100])

(*  True  *)


Here's a copy + paste from my init.m file:

JordanTotient[1, n_] := EulerPhi[n]

JordanTotient[_, 0] = 0;

JordanTotient[_, -1|1] = 1;

JordanTotient[k_, n_Integer] :=
With[{pdiv = PrimeDivisors[n]},
Abs[n]^k Product[1 - 1/p^k, {p, pdiv}] /; ListQ[pdiv]
]

JordanTotient /: MakeBoxes[JordanTotient[k_, n_], TraditionalForm] :=

(* utilities *)

Options[PrimeDivisors] = Options[FactorInteger];

PrimeDivisors[n_, ops___] :=
Block[{ps = FactorInteger[n, ops]},
(
ps = ps[[All, 1]];
If[Abs[First[ps]] > 1, ps, Rest[ps]]

) /; ListQ[ps]
]


The TraditionalForm formatting could be improved.