From c32fce9ffaf4a142430edce4cc50349cc84fca9e Mon Sep 17 00:00:00 2001 From: tianna0 <144874312+tianna0@users.noreply.github.com> Date: Tue, 21 Nov 2023 15:37:39 -0600 Subject: [PATCH] add Euler's Totient Function --- .../number_theory/Eulers_Totient_Function.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 algorithms/Python/number_theory/Eulers_Totient_Function.py diff --git a/algorithms/Python/number_theory/Eulers_Totient_Function.py b/algorithms/Python/number_theory/Eulers_Totient_Function.py new file mode 100644 index 00000000..c2b5066d --- /dev/null +++ b/algorithms/Python/number_theory/Eulers_Totient_Function.py @@ -0,0 +1,23 @@ +# counts the positive integers up to a given integer n that are relatively prime to n + +def euler_totient(n): + result = n + p = 2 + + while p * p <= n: + if n % p == 0: + while n % p == 0: + n //= p + result *= (1.0 - (1.0 / float(p))) + p += 1 + + if n > 1: + result *= (1.0 - (1.0 / float(n))) + + return int(result) +# TIME COMPLEXITY - O(sqrt(N)) + +# Example usage +#number = 10 ———> 4 + +