-2
$\begingroup$

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

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

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  *)
$\endgroup$
6
$\begingroup$

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] := 
  MakeBoxes[Subscript[J, 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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.