Skip to content

Commit ee749bd

Browse files
Added EulersTotientFunction function to the Maths Folder
1 parent 74f9bfb commit ee749bd

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

Maths/EulersTotientFunction.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
author sandyboypraper
3+
4+
Here is the EulerTotientFunction.
5+
it is also represented by phi
6+
7+
so EulersTotientFunction(n) (or phi(n)) is the count of numbers in {1,2,3,....,n} that are relatively
8+
prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1.
9+
*/
10+
11+
const gcd_two_numbers = (x, y) => {
12+
// x is smaller than y
13+
// let gcd of x and y is gcdXY
14+
// so it devides x and y completely
15+
// 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))
16+
// and gcd(x,y) is equals to gcd(y%x , x)
17+
return x == 0 ? y : gcd_two_numbers(y%x , x);
18+
}
19+
20+
const EulersTotientFunction = (n) => {
21+
let countOfRelativelyPrimeNumbers = 1;
22+
for(let iterator = 2; iterator<=n; iterator++)
23+
if(gcd_two_numbers(iterator , n) == 1)countOfRelativelyPrimeNumbers++;
24+
25+
return countOfRelativelyPrimeNumbers;
26+
}
27+
28+
export {EulersTotientFunction}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { EulersTotientFunction } from '../EulersTotientFunction';
2+
3+
describe('eulersTotientFunction', () => {
4+
it('is a function', () => {
5+
expect(typeof EulersTotientFunction).toEqual('function')
6+
})
7+
it('should return the phi of a given number', () => {
8+
const phiOfNumber = EulersTotientFunction(10)
9+
expect(phiOfNumber).toBe(4)
10+
})
11+
})

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
"jest": "^26.4.2",
2828
"standard": "^14.3.4"
2929
}
30-
}
30+
}

0 commit comments

Comments
 (0)