mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
some changes in coding style to make it standerize
This commit is contained in:
@ -8,21 +8,21 @@
|
|||||||
prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1.
|
prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const gcd_two_numbers = (x, y) => {
|
const gcdOfTwoNumbers = (x, y) => {
|
||||||
// x is smaller than y
|
// x is smaller than y
|
||||||
// let gcd of x and y is gcdXY
|
// let gcd of x and y is gcdXY
|
||||||
// so it devides x and y completely
|
// 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))
|
// 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)
|
// and gcd(x,y) is equals to gcd(y%x , x)
|
||||||
return x == 0 ? y : gcd_two_numbers(y%x , x);
|
return x === 0 ? y : gcdOfTwoNumbers(y % x, x)
|
||||||
}
|
}
|
||||||
|
|
||||||
const EulersTotientFunction = (n) => {
|
const eulersTotientFunction = (n) => {
|
||||||
let countOfRelativelyPrimeNumbers = 1;
|
let countOfRelativelyPrimeNumbers = 1
|
||||||
for(let iterator = 2; iterator<=n; iterator++)
|
for (let iterator = 2; iterator <= n; iterator++){
|
||||||
if(gcd_two_numbers(iterator , n) == 1)countOfRelativelyPrimeNumbers++;
|
if (gcdOfTwoNumbers(iterator, n) === 1)countOfRelativelyPrimeNumbers++
|
||||||
|
}
|
||||||
return countOfRelativelyPrimeNumbers;
|
return countOfRelativelyPrimeNumbers
|
||||||
}
|
}
|
||||||
|
|
||||||
export {EulersTotientFunction}
|
export { eulersTotientFunction }
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import { EulersTotientFunction } from '../EulersTotientFunction';
|
import { eulersTotientFunction } from '../EulersTotientFunction'
|
||||||
|
|
||||||
describe('eulersTotientFunction', () => {
|
describe('eulersTotientFunction', () => {
|
||||||
it('is a function', () => {
|
it('is a function', () => {
|
||||||
expect(typeof EulersTotientFunction).toEqual('function')
|
expect(typeof eulersTotientFunction).toEqual('function')
|
||||||
})
|
})
|
||||||
it('should return the phi of a given number', () => {
|
it('should return the phi of a given number', () => {
|
||||||
const phiOfNumber = EulersTotientFunction(10)
|
const phiOfNumber = eulersTotientFunction(10)
|
||||||
expect(phiOfNumber).toBe(4)
|
expect(phiOfNumber).toBe(4)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user