From b7309f0018722801232f9de6219666e6ef69fd32 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Fri, 7 Oct 2022 20:36:47 +0530 Subject: [PATCH 01/12] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20Added=20solution=20?= =?UTF-8?q?for=20ProjectEuler-007?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem007.js | 27 +++++++++++++++++++++++++++ Project-Euler/test/Problem007.test.js | 17 +++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 Project-Euler/Problem007.js create mode 100644 Project-Euler/test/Problem007.test.js diff --git a/Project-Euler/Problem007.js b/Project-Euler/Problem007.js new file mode 100644 index 0000000000..48f27d56c1 --- /dev/null +++ b/Project-Euler/Problem007.js @@ -0,0 +1,27 @@ +import { PrimeCheck } from '../Maths/PrimeCheck.js' + +/** + * Find nth Prime Number + * + * P.S.(Project Euler - 007): + * By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. + * What is the 10001st prime number? + * + * @param {Number} n + * @returns {Number} returns the nth prime number + */ +export const nthPrime = (n) => { + if (n < 1) { + return 'Invalid Input' + } + + let count = 0 + let inc = 2 + while (count < n) { + if (PrimeCheck(inc)) { + count++ + } + inc++ + } + return inc - 1 +} diff --git a/Project-Euler/test/Problem007.test.js b/Project-Euler/test/Problem007.test.js new file mode 100644 index 0000000000..e979e45728 --- /dev/null +++ b/Project-Euler/test/Problem007.test.js @@ -0,0 +1,17 @@ +import { nthPrime } from '../Problem007.js' + +describe('checking nth prime number', () => { + it('should be invalid input if number is negative', () => { + expect(nthPrime(-3)).toBe('Invalid Input') + }) + it('should be invalid input if number is 0', () => { + expect(nthPrime(0)).toBe('Invalid Input') + }) + test('if the number is greather than 0', () => { + expect(nthPrime(10)).toBe(29) + }) + // Project Euler Condition Check + test('if the number is 10001', () => { + expect(nthPrime(10001)).toBe(104743) + }) +}) From d3a3b331c7d964262f524c37afb767c87994b6c3 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Fri, 7 Oct 2022 20:45:06 +0530 Subject: [PATCH 02/12] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Spelling=20mistake?= =?UTF-8?q?=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/test/Problem007.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project-Euler/test/Problem007.test.js b/Project-Euler/test/Problem007.test.js index e979e45728..6c48f1e786 100644 --- a/Project-Euler/test/Problem007.test.js +++ b/Project-Euler/test/Problem007.test.js @@ -7,7 +7,7 @@ describe('checking nth prime number', () => { it('should be invalid input if number is 0', () => { expect(nthPrime(0)).toBe('Invalid Input') }) - test('if the number is greather than 0', () => { + test('if the number is greater than 0', () => { expect(nthPrime(10)).toBe(29) }) // Project Euler Condition Check From e9b5a6a92174ff87a4d9b27cee8e596ad8fa76a3 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Fri, 7 Oct 2022 23:02:44 +0530 Subject: [PATCH 03/12] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20changed=20varia?= =?UTF-8?q?ble=20name=20from=20`inc`=20to=20`candidateValue`=20and=20throw?= =?UTF-8?q?n=20error=20in=20case=20of=20invalid=20input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem007.js | 10 +++++----- Project-Euler/test/Problem007.test.js | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Project-Euler/Problem007.js b/Project-Euler/Problem007.js index 48f27d56c1..95e3f10562 100644 --- a/Project-Euler/Problem007.js +++ b/Project-Euler/Problem007.js @@ -12,16 +12,16 @@ import { PrimeCheck } from '../Maths/PrimeCheck.js' */ export const nthPrime = (n) => { if (n < 1) { - return 'Invalid Input' + throw new Error('Invalid Input') } let count = 0 - let inc = 2 + let candidateValue = 2 while (count < n) { - if (PrimeCheck(inc)) { + if (PrimeCheck(candidateValue)) { count++ } - inc++ + candidateValue++ } - return inc - 1 + return candidateValue - 1 } diff --git a/Project-Euler/test/Problem007.test.js b/Project-Euler/test/Problem007.test.js index 6c48f1e786..191d1e06af 100644 --- a/Project-Euler/test/Problem007.test.js +++ b/Project-Euler/test/Problem007.test.js @@ -2,10 +2,10 @@ import { nthPrime } from '../Problem007.js' describe('checking nth prime number', () => { it('should be invalid input if number is negative', () => { - expect(nthPrime(-3)).toBe('Invalid Input') + expect(() => nthPrime(-3)).toThrowError('Invalid Input') }) it('should be invalid input if number is 0', () => { - expect(nthPrime(0)).toBe('Invalid Input') + expect(() => nthPrime(0)).toThrowError('Invalid Input') }) test('if the number is greater than 0', () => { expect(nthPrime(10)).toBe(29) From e99c7225579aa6da265352daa5140c037281c311 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Fri, 7 Oct 2022 23:23:09 +0530 Subject: [PATCH 04/12] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Modified=20the?= =?UTF-8?q?=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem007.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Project-Euler/Problem007.js b/Project-Euler/Problem007.js index 95e3f10562..009c1a896e 100644 --- a/Project-Euler/Problem007.js +++ b/Project-Euler/Problem007.js @@ -16,12 +16,12 @@ export const nthPrime = (n) => { } let count = 0 - let candidateValue = 2 + let candidateValue = 1 while (count < n) { + candidateValue++ if (PrimeCheck(candidateValue)) { count++ } - candidateValue++ } - return candidateValue - 1 + return candidateValue } From 0f9f1bab7decbea8a2fbfc06b4b8531a39d0d6e1 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Mon, 10 Oct 2022 19:50:16 +0530 Subject: [PATCH 05/12] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Added=20test=20?= =?UTF-8?q?case=20for=20ProjectEuler=20Problem001?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem001.js | 4 +++- Project-Euler/test/Problem001.test.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 Project-Euler/test/Problem001.test.js diff --git a/Project-Euler/Problem001.js b/Project-Euler/Problem001.js index 65a35e32a1..c47c10bd1c 100644 --- a/Project-Euler/Problem001.js +++ b/Project-Euler/Problem001.js @@ -5,9 +5,11 @@ Find the sum of all the multiples of 3 or 5 below the provided parameter value n */ const multiplesThreeAndFive = (num) => { + if (num < 1) throw new Error('No natural numbers exist below 1') + let total = 0 // total for calculating the sum - for (let i = 0; i < num; i++) { + for (let i = 1; i < num; i++) { if (i % 3 === 0 || i % 5 === 0) { total += i } diff --git a/Project-Euler/test/Problem001.test.js b/Project-Euler/test/Problem001.test.js new file mode 100644 index 0000000000..e6b5549a57 --- /dev/null +++ b/Project-Euler/test/Problem001.test.js @@ -0,0 +1,17 @@ +import { multiplesThreeAndFive } from '../Problem001.js' + +describe('Sum of multiples of 3 or 5', () => { + it('should throw error when number is negative number', () => { + expect(() => multiplesThreeAndFive(-24)).toThrowError('No natural numbers exist below 1') + }) + it('should throw error when number is 0', () => { + expect(() => multiplesThreeAndFive(0)).toThrowError('No natural numbers exist below 1') + }) + test('if the number is greater than 0', () => { + expect(multiplesThreeAndFive(10)).toBe(23) + }) + // Project Euler Condition Check + test('if the number is 1000', () => { + expect(multiplesThreeAndFive(1000)).toBe(233168) + }) +}) From b06c09b1ba7edb948af33b75ee9612972f79d9e3 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sat, 15 Oct 2022 16:11:56 +0530 Subject: [PATCH 06/12] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20Added=20solution=20?= =?UTF-8?q?for=20Project=20Euler-021?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem021.js | 45 +++++++++++++++++++++++++++ Project-Euler/test/Problem021.test.js | 8 +++++ 2 files changed, 53 insertions(+) create mode 100644 Project-Euler/Problem021.js create mode 100644 Project-Euler/test/Problem021.test.js diff --git a/Project-Euler/Problem021.js b/Project-Euler/Problem021.js new file mode 100644 index 0000000000..45ff9e1552 --- /dev/null +++ b/Project-Euler/Problem021.js @@ -0,0 +1,45 @@ +/** + * Amicable numbers + * + * P.S.(Project Euler - 021): + * Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). + * If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers. + * + * For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; + * therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220. + * + * Evaluate the sum of all the amicable numbers under 10000. + * + * @param {Number} maxNum + * @returns {Number} Sum of all amicable numbers under maxNum + */ +export const sumAmicableNum = (maxNum) => { + const amicNums = [] + + for (let i = 1; i < maxNum; i++) { + const left = d(i) + const right = d(left) + if (i === right && i !== left && amicNums.indexOf(i) === -1) { + amicNums.push(i, left) + } + } + return amicNums.reduce((a, b) => { + a += b + return a + }) +} + +const d = (num) => { + const output = [] + for (let i = 1; i < num; i++) { + if (!(num % i)) { output.push(i) } + } + if (!output.length) { + return 0 + } else { + return output.reduce((a, b) => { + a += b + return a + }) + } +} diff --git a/Project-Euler/test/Problem021.test.js b/Project-Euler/test/Problem021.test.js new file mode 100644 index 0000000000..fed82d362c --- /dev/null +++ b/Project-Euler/test/Problem021.test.js @@ -0,0 +1,8 @@ +import { sumAmicableNum } from '../Problem021.js' + +describe('Amicable numbers', () => { + // Project Euler Condition Check + test('if the number is 10000', () => { + expect(sumAmicableNum(10000)).toBe(31626) + }) +}) From 959b0b62397f74b6851ee155d09dfc22e13b1dfe Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 15 Oct 2022 10:42:57 +0000 Subject: [PATCH 07/12] Updated Documentation in README.md --- DIRECTORY.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2eec03b7b6..c7fceb2d76 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -9,6 +9,7 @@ * **Bit-Manipulation** * [BinaryCountSetBits](Bit-Manipulation/BinaryCountSetBits.js) * [IsPowerOfTwo](Bit-Manipulation/IsPowerOfTwo.js) + * [LogTwo](Bit-Manipulation/LogTwo.js) * [NextPowerOfTwo](Bit-Manipulation/NextPowerOfTwo.js) * [SetBit](Bit-Manipulation/SetBit.js) * **Cache** @@ -20,7 +21,7 @@ * **Ciphers** * [AffineCipher](Ciphers/AffineCipher.js) * [Atbash](Ciphers/Atbash.js) - * [CaesarsCipher](Ciphers/CaesarsCipher.js) + * [CaesarCipher](Ciphers/CaesarCipher.js) * [KeyFinder](Ciphers/KeyFinder.js) * [KeywordShiftedAlphabet](Ciphers/KeywordShiftedAlphabet.js) * [ROT13](Ciphers/ROT13.js) @@ -68,7 +69,6 @@ * [AddTwoNumbers](Data-Structures/Linked-List/AddTwoNumbers.js) * [CycleDetection](Data-Structures/Linked-List/CycleDetection.js) * [DoublyLinkedList](Data-Structures/Linked-List/DoublyLinkedList.js) - * [RotateListRight](Data-Structures/Linked-List/RotateListRight.js) * [SinglyCircularLinkedList](Data-Structures/Linked-List/SinglyCircularLinkedList.js) * [SinglyLinkedList](Data-Structures/Linked-List/SinglyLinkedList.js) * **Queue** @@ -85,6 +85,7 @@ * **Vectors** * [Vector2](Data-Structures/Vectors/Vector2.js) * **Dynamic-Programming** + * [CatalanNumbers](Dynamic-Programming/CatalanNumbers.js) * [ClimbingStairs](Dynamic-Programming/ClimbingStairs.js) * [CoinChange](Dynamic-Programming/CoinChange.js) * [EditDistance](Dynamic-Programming/EditDistance.js) @@ -141,8 +142,10 @@ * [BinaryConvert](Maths/BinaryConvert.js) * [BinaryExponentiationIterative](Maths/BinaryExponentiationIterative.js) * [BinaryExponentiationRecursive](Maths/BinaryExponentiationRecursive.js) + * [BinomialCoefficient](Maths/BinomialCoefficient.js) * [BisectionMethod](Maths/BisectionMethod.js) * [CheckKishnamurthyNumber](Maths/CheckKishnamurthyNumber.js) + * [CircularArc](Maths/CircularArc.js) * [CollatzSequence](Maths/CollatzSequence.js) * [Coordinate](Maths/Coordinate.js) * [CoPrimeCheck](Maths/CoPrimeCheck.js) @@ -170,15 +173,18 @@ * [IsEven](Maths/IsEven.js) * [IsOdd](Maths/IsOdd.js) * [IsPronic](Maths/IsPronic.js) + * [IsSquareFree](Maths/IsSquareFree.js) * [JugglerSequence](Maths/JugglerSequence.js) * [LeapYear](Maths/LeapYear.js) * [LinearSieve](Maths/LinearSieve.js) + * [LiouvilleFunction](Maths/LiouvilleFunction.js) * [LucasSeries](Maths/LucasSeries.js) * [Mandelbrot](Maths/Mandelbrot.js) * [MatrixExponentiationRecursive](Maths/MatrixExponentiationRecursive.js) * [MatrixMultiplication](Maths/MatrixMultiplication.js) * [MeanSquareError](Maths/MeanSquareError.js) * [MidpointIntegration](Maths/MidpointIntegration.js) + * [MobiusFunction](Maths/MobiusFunction.js) * [ModularBinaryExponentiationRecursive](Maths/ModularBinaryExponentiationRecursive.js) * [NumberOfDigits](Maths/NumberOfDigits.js) * [Palindrome](Maths/Palindrome.js) @@ -216,6 +222,7 @@ * [Problem004](Project-Euler/Problem004.js) * [Problem005](Project-Euler/Problem005.js) * [Problem006](Project-Euler/Problem006.js) + * [Problem007](Project-Euler/Problem007.js) * [Problem008](Project-Euler/Problem008.js) * [Problem009](Project-Euler/Problem009.js) * [Problem010](Project-Euler/Problem010.js) @@ -225,6 +232,7 @@ * [Problem016](Project-Euler/Problem016.js) * [Problem018](Project-Euler/Problem018.js) * [Problem020](Project-Euler/Problem020.js) + * [Problem021](Project-Euler/Problem021.js) * [Problem023](Project-Euler/Problem023.js) * [Problem025](Project-Euler/Problem025.js) * **Recursive** @@ -245,6 +253,7 @@ * [InterpolationSearch](Search/InterpolationSearch.js) * [JumpSearch](Search/JumpSearch.js) * [LinearSearch](Search/LinearSearch.js) + * [Minesweeper](Search/Minesweeper.js) * [QuickSelectSearch](Search/QuickSelectSearch.js) * [SlidingWindow](Search/SlidingWindow.js) * [StringSearch](Search/StringSearch.js) @@ -296,6 +305,7 @@ * [CheckRearrangePalindrome](String/CheckRearrangePalindrome.js) * [CheckSnakeCase](String/CheckSnakeCase.js) * [CheckWordOccurrence](String/CheckWordOccurrence.js) + * [CountSubstrings](String/CountSubstrings.js) * [CountVowels](String/CountVowels.js) * [CreatePermutations](String/CreatePermutations.js) * [DiceCoefficient](String/DiceCoefficient.js) From 35ecee1526d0c1d3e3cdaab9c6402b02299b6b17 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 15 Oct 2022 10:48:14 +0000 Subject: [PATCH 08/12] Updated Documentation in README.md --- DIRECTORY.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 603a367734..e78a122fbc 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -2,7 +2,7 @@ * [AllCombinationsOfSizeK](Backtracking/AllCombinationsOfSizeK.js) * [GeneratePermutations](Backtracking/GeneratePermutations.js) * [KnightTour](Backtracking/KnightTour.js) - * [NQueen](Backtracking/NQueen.js) + * [NQueens](Backtracking/NQueens.js) * [RatInAMaze](Backtracking/RatInAMaze.js) * [Sudoku](Backtracking/Sudoku.js) * [SumOfSubset](Backtracking/SumOfSubset.js) @@ -149,6 +149,7 @@ * [CollatzSequence](Maths/CollatzSequence.js) * [Coordinate](Maths/Coordinate.js) * [CoPrimeCheck](Maths/CoPrimeCheck.js) + * [CountNumbersDivisible](Maths/CountNumbersDivisible.js) * [DecimalExpansion](Maths/DecimalExpansion.js) * [DecimalIsolate](Maths/DecimalIsolate.js) * [DegreeToRadian](Maths/DegreeToRadian.js) @@ -172,6 +173,7 @@ * [IsDivisible](Maths/IsDivisible.js) * [IsEven](Maths/IsEven.js) * [IsOdd](Maths/IsOdd.js) + * [isPalindromeIntegerNumber](Maths/isPalindromeIntegerNumber.js) * [IsPronic](Maths/IsPronic.js) * [IsSquareFree](Maths/IsSquareFree.js) * [JugglerSequence](Maths/JugglerSequence.js) @@ -227,6 +229,7 @@ * [Problem008](Project-Euler/Problem008.js) * [Problem009](Project-Euler/Problem009.js) * [Problem010](Project-Euler/Problem010.js) + * [Problem011](Project-Euler/Problem011.js) * [Problem012](Project-Euler/Problem012.js) * [Problem013](Project-Euler/Problem013.js) * [Problem014](Project-Euler/Problem014.js) @@ -238,6 +241,7 @@ * [Problem021](Project-Euler/Problem021.js) * [Problem023](Project-Euler/Problem023.js) * [Problem025](Project-Euler/Problem025.js) + * [Problem044](Project-Euler/Problem044.js) * **Recursive** * [BinaryEquivalent](Recursive/BinaryEquivalent.js) * [BinarySearch](Recursive/BinarySearch.js) @@ -291,6 +295,7 @@ * [ShellSort](Sorts/ShellSort.js) * [SimplifiedWiggleSort](Sorts/SimplifiedWiggleSort.js) * [StoogeSort](Sorts/StoogeSort.js) + * [SwapSort](Sorts/SwapSort.js) * [TimSort](Sorts/TimSort.js) * [TopologicalSort](Sorts/TopologicalSort.js) * **String** From d6eb962ee0f69b28a1d931c8eec7940091386f59 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Mon, 17 Oct 2022 23:08:28 +0530 Subject: [PATCH 09/12] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Some=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem021.js | 18 ++++-------------- Project-Euler/test/Problem021.test.js | 6 +++--- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/Project-Euler/Problem021.js b/Project-Euler/Problem021.js index 45ff9e1552..c97bacd3b0 100644 --- a/Project-Euler/Problem021.js +++ b/Project-Euler/Problem021.js @@ -13,7 +13,7 @@ * @param {Number} maxNum * @returns {Number} Sum of all amicable numbers under maxNum */ -export const sumAmicableNum = (maxNum) => { +export const sumAmicableNums = (maxNum) => { const amicNums = [] for (let i = 1; i < maxNum; i++) { @@ -23,23 +23,13 @@ export const sumAmicableNum = (maxNum) => { amicNums.push(i, left) } } - return amicNums.reduce((a, b) => { - a += b - return a - }) + return amicNums.reduce((a, b) => a + b, 0) } const d = (num) => { const output = [] for (let i = 1; i < num; i++) { - if (!(num % i)) { output.push(i) } - } - if (!output.length) { - return 0 - } else { - return output.reduce((a, b) => { - a += b - return a - }) + if (num % i === 0) { output.push(i) } } + return output.reduce((a, b) => a + b, 0) } diff --git a/Project-Euler/test/Problem021.test.js b/Project-Euler/test/Problem021.test.js index fed82d362c..f907c1553f 100644 --- a/Project-Euler/test/Problem021.test.js +++ b/Project-Euler/test/Problem021.test.js @@ -1,8 +1,8 @@ -import { sumAmicableNum } from '../Problem021.js' +import { sumAmicableNums } from '../Problem021.js' describe('Amicable numbers', () => { // Project Euler Condition Check - test('if the number is 10000', () => { - expect(sumAmicableNum(10000)).toBe(31626) + test('if the limit is 10000', () => { + expect(sumAmicableNums(10000)).toBe(31626) }) }) From 4017f5587b35c9bde537a6426d9342be15009907 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sat, 15 Oct 2022 16:11:56 +0530 Subject: [PATCH 10/12] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20Added=20solution=20?= =?UTF-8?q?for=20Project=20Euler-021?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem021.js | 45 +++++++++++++++++++++++++++ Project-Euler/test/Problem021.test.js | 8 +++++ 2 files changed, 53 insertions(+) create mode 100644 Project-Euler/Problem021.js create mode 100644 Project-Euler/test/Problem021.test.js diff --git a/Project-Euler/Problem021.js b/Project-Euler/Problem021.js new file mode 100644 index 0000000000..45ff9e1552 --- /dev/null +++ b/Project-Euler/Problem021.js @@ -0,0 +1,45 @@ +/** + * Amicable numbers + * + * P.S.(Project Euler - 021): + * Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). + * If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers. + * + * For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; + * therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220. + * + * Evaluate the sum of all the amicable numbers under 10000. + * + * @param {Number} maxNum + * @returns {Number} Sum of all amicable numbers under maxNum + */ +export const sumAmicableNum = (maxNum) => { + const amicNums = [] + + for (let i = 1; i < maxNum; i++) { + const left = d(i) + const right = d(left) + if (i === right && i !== left && amicNums.indexOf(i) === -1) { + amicNums.push(i, left) + } + } + return amicNums.reduce((a, b) => { + a += b + return a + }) +} + +const d = (num) => { + const output = [] + for (let i = 1; i < num; i++) { + if (!(num % i)) { output.push(i) } + } + if (!output.length) { + return 0 + } else { + return output.reduce((a, b) => { + a += b + return a + }) + } +} diff --git a/Project-Euler/test/Problem021.test.js b/Project-Euler/test/Problem021.test.js new file mode 100644 index 0000000000..fed82d362c --- /dev/null +++ b/Project-Euler/test/Problem021.test.js @@ -0,0 +1,8 @@ +import { sumAmicableNum } from '../Problem021.js' + +describe('Amicable numbers', () => { + // Project Euler Condition Check + test('if the number is 10000', () => { + expect(sumAmicableNum(10000)).toBe(31626) + }) +}) From a33da9aa724b9b38a70ef380cb890842cf683544 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 15 Oct 2022 10:42:57 +0000 Subject: [PATCH 11/12] Updated Documentation in README.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index e1bbd77000..c86cccec18 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -239,6 +239,7 @@ * [Problem017](Project-Euler/Problem017.js) * [Problem018](Project-Euler/Problem018.js) * [Problem020](Project-Euler/Problem020.js) + * [Problem021](Project-Euler/Problem021.js) * [Problem023](Project-Euler/Problem023.js) * [Problem025](Project-Euler/Problem025.js) * [Problem028](Project-Euler/Problem028.js) From 7fd38e6b128943029980b8b16c7887f068038b3e Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Mon, 17 Oct 2022 23:08:28 +0530 Subject: [PATCH 12/12] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Some=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem021.js | 18 ++++-------------- Project-Euler/test/Problem021.test.js | 6 +++--- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/Project-Euler/Problem021.js b/Project-Euler/Problem021.js index 45ff9e1552..c97bacd3b0 100644 --- a/Project-Euler/Problem021.js +++ b/Project-Euler/Problem021.js @@ -13,7 +13,7 @@ * @param {Number} maxNum * @returns {Number} Sum of all amicable numbers under maxNum */ -export const sumAmicableNum = (maxNum) => { +export const sumAmicableNums = (maxNum) => { const amicNums = [] for (let i = 1; i < maxNum; i++) { @@ -23,23 +23,13 @@ export const sumAmicableNum = (maxNum) => { amicNums.push(i, left) } } - return amicNums.reduce((a, b) => { - a += b - return a - }) + return amicNums.reduce((a, b) => a + b, 0) } const d = (num) => { const output = [] for (let i = 1; i < num; i++) { - if (!(num % i)) { output.push(i) } - } - if (!output.length) { - return 0 - } else { - return output.reduce((a, b) => { - a += b - return a - }) + if (num % i === 0) { output.push(i) } } + return output.reduce((a, b) => a + b, 0) } diff --git a/Project-Euler/test/Problem021.test.js b/Project-Euler/test/Problem021.test.js index fed82d362c..f907c1553f 100644 --- a/Project-Euler/test/Problem021.test.js +++ b/Project-Euler/test/Problem021.test.js @@ -1,8 +1,8 @@ -import { sumAmicableNum } from '../Problem021.js' +import { sumAmicableNums } from '../Problem021.js' describe('Amicable numbers', () => { // Project Euler Condition Check - test('if the number is 10000', () => { - expect(sumAmicableNum(10000)).toBe(31626) + test('if the limit is 10000', () => { + expect(sumAmicableNums(10000)).toBe(31626) }) })