summaryrefslogtreecommitdiff
path: root/math/phi.cpp
diff options
context:
space:
mode:
authorPaul Jungeblut <paul.jungeblut@gmail.com>2016-10-15 22:48:49 +0200
committerPaul Jungeblut <paul.jungeblut@gmail.com>2016-10-15 22:48:49 +0200
commit53c8e56d8b0ee3b4374ab90630673b971ddf2710 (patch)
treed82399f0bc3dfff6c6ebe11fba55282b058780aa /math/phi.cpp
parenta7e23f85ac2c02a4656277348f5546ebd3e6b303 (diff)
Fast factorization method and Euler's phi function
Diffstat (limited to 'math/phi.cpp')
-rw-r--r--math/phi.cpp9
1 files changed, 9 insertions, 0 deletions
diff --git a/math/phi.cpp b/math/phi.cpp
new file mode 100644
index 0000000..c8f8900
--- /dev/null
+++ b/math/phi.cpp
@@ -0,0 +1,9 @@
+ll phi(ll n) {
+ ll result = n;
+ for(int i = 2; i * i <= n; ++i) if(n % i == 0) {
+ while(n % i == 0)n /= i;
+ result -= result / i;
+ }
+ if(n > 1) result -= result / n;
+ return result;
+}