Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 28 additions & 0 deletions Maths/EulersTotientFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
author sandyboypraper

Here is the EulerTotientFunction.
it is also represented by phi

so EulersTotientFunction(n) (or phi(n)) is the count of numbers in {1,2,3,....,n} that are relatively
prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1.
*/

const gcdOfTwoNumbers = (x, y) => {
// x is smaller than y
// let gcd of x and y is gcdXY
// so it devides x and y completely
// so gcdXY should also devides y%x (y = gcdXY*a and x = gcdXY*b and y%x = y - x*k so y%x = gcdXY(a - b*k))
// and gcd(x,y) is equals to gcd(y%x , x)
return x === 0 ? y : gcdOfTwoNumbers(y % x, x)
}

const eulersTotientFunction = (n) => {
let countOfRelativelyPrimeNumbers = 1
for (let iterator = 2; iterator <= n; iterator++) {
if (gcdOfTwoNumbers(iterator, n) === 1) countOfRelativelyPrimeNumbers++
}
return countOfRelativelyPrimeNumbers
}

export { eulersTotientFunction }
11 changes: 11 additions & 0 deletions Maths/test/EulersTotientFunction.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { eulersTotientFunction } from '../EulersTotientFunction'

describe('eulersTotientFunction', () => {
it('is a function', () => {
expect(typeof eulersTotientFunction).toEqual('function')
})
it('should return the phi of a given number', () => {
const phiOfNumber = eulersTotientFunction(10)
expect(phiOfNumber).toBe(4)
})
})
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
"jest": "^26.4.2",
"standard": "^14.3.4"
}
}
}