Skip to content

Commit

Permalink
EulerPhi and PowerMod.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Nov 2, 2017
1 parent 219c991 commit 7774064
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions expreduce/builtin_numbertheory.go
Expand Up @@ -149,5 +149,7 @@ func GetNumberTheoryDefinitions() (defs []Definition) {
defs = append(defs, Definition{Name: "FactorInteger"})
defs = append(defs, Definition{Name: "FractionalPart"})
defs = append(defs, Definition{Name: "IntegerPart"})
defs = append(defs, Definition{Name: "PowerMod"})
defs = append(defs, Definition{Name: "EulerPhi"})
return
}
4 changes: 2 additions & 2 deletions expreduce/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions expreduce/resources/numbertheory.m
Expand Up @@ -224,3 +224,31 @@
ESameTest[1,IntegerPart[1]],
]
};

PowerMod::usage = "`PowerMod[x, y, m]` computes `Mod[x^y, m]`";
(*TODO: use efficient version of this function.*)
PowerMod[x_, y_, m_] := Mod[x^y, m];
Attributes[PowerMod] = {Listable, Protected, ReadProtected};
Tests`PowerMod = {
ESimpleExamples[
ESameTest[6,PowerMod[5, 9999, 7]],
]
};

EulerPhi::usage = "`EulerPhi[n]` computes Euler's totient function for `n`";
Attributes[EulerPhi] = {Listable, Protected, ReadProtected};
EulerPhi[0] := 0;
EulerPhi[n_Integer?Positive] :=
If[n === 1, 1,
n*Product[1 - 1/p[[1]], {p, FactorInteger[n]}]];
EulerPhi[n_Integer?Negative] := EulerPhi[-n];
Tests`EulerPhi = {
ESimpleExamples[
ESameTest[42,EulerPhi[98]],
ESameTest[0,EulerPhi[0]],
ESameTest[42,EulerPhi[-98]],
], ETests[
ESameTest[1,EulerPhi[1]],
ESameTest[1,EulerPhi[-1]],
]
};

0 comments on commit 7774064

Please sign in to comment.