Skip to content

Added EulersTotient #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Maths/EulersTotient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Source:
https://en.wikipedia.org/wiki/Euler%27s_totient_function

EulersTotient(n) = n * product(1 - 1/p for all prime p dividing n)

Complexity:
O(sqrt(n))
*/

const EulersTotient = (n) => {
// input: n: int
// output: phi(n): count of numbers b/w 1 and n that are coprime to n
let res = n
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) {
while (n % i === 0) {
n = Math.floor(n / i)
}
// i is a prime diving n, multiply res by 1 - 1/i
// res = res * (1 - 1/i) = res - (res / i)
res = res - Math.floor(res / i)
}
}
if (n > 1) {
res = res - Math.floor(res / n)
}
return res
}

const main = () => {
// EulersTotient(9) = 6 as 1, 2, 4, 5, 7, and 8 are coprime to 9
// > 6
console.log(EulersTotient(9))
// EulersTotient(10) = 4 as 1, 3, 7, 9 are coprime to 10
// > 4
console.log(EulersTotient(10))
}

main()