From ee749bde471d19df65f336d7107a71a08c1345d2 Mon Sep 17 00:00:00 2001 From: sandyboypraper Date: Mon, 14 Dec 2020 00:33:55 +0530 Subject: [PATCH 1/4] Added EulersTotientFunction function to the Maths Folder --- Maths/EulersTotientFunction.js | 28 ++++++++++++++++++++++++ Maths/test/EulersTotientFunction.test.js | 11 ++++++++++ package-lock.json | 2 +- package.json | 2 +- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 Maths/EulersTotientFunction.js create mode 100644 Maths/test/EulersTotientFunction.test.js diff --git a/Maths/EulersTotientFunction.js b/Maths/EulersTotientFunction.js new file mode 100644 index 000000000..ca8bafc0e --- /dev/null +++ b/Maths/EulersTotientFunction.js @@ -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 gcd_two_numbers = (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 : gcd_two_numbers(y%x , x); +} + +const EulersTotientFunction = (n) => { + let countOfRelativelyPrimeNumbers = 1; + for(let iterator = 2; iterator<=n; iterator++) + if(gcd_two_numbers(iterator , n) == 1)countOfRelativelyPrimeNumbers++; + + return countOfRelativelyPrimeNumbers; +} + +export {EulersTotientFunction} diff --git a/Maths/test/EulersTotientFunction.test.js b/Maths/test/EulersTotientFunction.test.js new file mode 100644 index 000000000..d58d57c4b --- /dev/null +++ b/Maths/test/EulersTotientFunction.test.js @@ -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) + }) +}) diff --git a/package-lock.json b/package-lock.json index d8c7400ba..f051f1f03 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9625,4 +9625,4 @@ } } } -} \ No newline at end of file +} diff --git a/package.json b/package.json index 3adff89e6..729e6f71b 100644 --- a/package.json +++ b/package.json @@ -27,4 +27,4 @@ "jest": "^26.4.2", "standard": "^14.3.4" } -} \ No newline at end of file +} From 4746d0c90ec49228e0698d88a74f46c46f64c1b8 Mon Sep 17 00:00:00 2001 From: sandyboypraper Date: Mon, 14 Dec 2020 12:26:08 +0530 Subject: [PATCH 2/4] some changes in coding style to make it standerize --- Maths/EulersTotientFunction.js | 28 ++++++++++++------------ Maths/test/EulersTotientFunction.test.js | 6 ++--- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Maths/EulersTotientFunction.js b/Maths/EulersTotientFunction.js index ca8bafc0e..338154d4e 100644 --- a/Maths/EulersTotientFunction.js +++ b/Maths/EulersTotientFunction.js @@ -8,21 +8,21 @@ prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. */ -const gcd_two_numbers = (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 : gcd_two_numbers(y%x , x); +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(gcd_two_numbers(iterator , n) == 1)countOfRelativelyPrimeNumbers++; - - return countOfRelativelyPrimeNumbers; +const eulersTotientFunction = (n) => { + let countOfRelativelyPrimeNumbers = 1 + for (let iterator = 2; iterator <= n; iterator++){ + if (gcdOfTwoNumbers(iterator, n) === 1)countOfRelativelyPrimeNumbers++ + } + return countOfRelativelyPrimeNumbers } -export {EulersTotientFunction} +export { eulersTotientFunction } diff --git a/Maths/test/EulersTotientFunction.test.js b/Maths/test/EulersTotientFunction.test.js index d58d57c4b..2b8d6c475 100644 --- a/Maths/test/EulersTotientFunction.test.js +++ b/Maths/test/EulersTotientFunction.test.js @@ -1,11 +1,11 @@ -import { EulersTotientFunction } from '../EulersTotientFunction'; +import { eulersTotientFunction } from '../EulersTotientFunction' describe('eulersTotientFunction', () => { it('is a function', () => { - expect(typeof EulersTotientFunction).toEqual('function') + expect(typeof eulersTotientFunction).toEqual('function') }) it('should return the phi of a given number', () => { - const phiOfNumber = EulersTotientFunction(10) + const phiOfNumber = eulersTotientFunction(10) expect(phiOfNumber).toBe(4) }) }) From 6d91a1eedefb016e36ccf30596d16d68ee843f33 Mon Sep 17 00:00:00 2001 From: sandyboypraper Date: Mon, 14 Dec 2020 12:30:03 +0530 Subject: [PATCH 3/4] some changes in coding style to make it standerize --- Maths/EulersTotientFunction.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/EulersTotientFunction.js b/Maths/EulersTotientFunction.js index 338154d4e..34377036d 100644 --- a/Maths/EulersTotientFunction.js +++ b/Maths/EulersTotientFunction.js @@ -19,7 +19,7 @@ const gcdOfTwoNumbers = (x, y) => { const eulersTotientFunction = (n) => { let countOfRelativelyPrimeNumbers = 1 - for (let iterator = 2; iterator <= n; iterator++){ + for ( let iterator = 2; iterator <= n; iterator++ ){ if (gcdOfTwoNumbers(iterator, n) === 1)countOfRelativelyPrimeNumbers++ } return countOfRelativelyPrimeNumbers From c931b4675f5f8cb3a87d4f8606d1689674b13081 Mon Sep 17 00:00:00 2001 From: sandyboypraper Date: Mon, 14 Dec 2020 12:38:08 +0530 Subject: [PATCH 4/4] some changes in coding style to make it standerize --- Maths/EulersTotientFunction.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/EulersTotientFunction.js b/Maths/EulersTotientFunction.js index 34377036d..90fd27450 100644 --- a/Maths/EulersTotientFunction.js +++ b/Maths/EulersTotientFunction.js @@ -19,8 +19,8 @@ const gcdOfTwoNumbers = (x, y) => { const eulersTotientFunction = (n) => { let countOfRelativelyPrimeNumbers = 1 - for ( let iterator = 2; iterator <= n; iterator++ ){ - if (gcdOfTwoNumbers(iterator, n) === 1)countOfRelativelyPrimeNumbers++ + for (let iterator = 2; iterator <= n; iterator++) { + if (gcdOfTwoNumbers(iterator, n) === 1) countOfRelativelyPrimeNumbers++ } return countOfRelativelyPrimeNumbers }