|
8 | 8 | prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. |
9 | 9 | */ |
10 | 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); |
| 11 | +const gcdOfTwoNumbers = (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 : gcdOfTwoNumbers(y % x, x) |
18 | 18 | } |
19 | 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; |
| 20 | +const eulersTotientFunction = (n) => { |
| 21 | + let countOfRelativelyPrimeNumbers = 1 |
| 22 | + for (let iterator = 2; iterator <= n; iterator++){ |
| 23 | + if (gcdOfTwoNumbers(iterator, n) === 1)countOfRelativelyPrimeNumbers++ |
| 24 | + } |
| 25 | + return countOfRelativelyPrimeNumbers |
26 | 26 | } |
27 | 27 |
|
28 | | -export {EulersTotientFunction} |
| 28 | +export { eulersTotientFunction } |
0 commit comments