From 4b5bddb5e496ce8f4383b31a6453f86d0724a709 Mon Sep 17 00:00:00 2001 From: ddaniel27 <67126972+ddaniel27@users.noreply.github.com> Date: Thu, 13 Oct 2022 21:10:35 -0500 Subject: [PATCH 1/5] [CREATE] Problem 044 from Project Euler --- Project-Euler/Problem044.js | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Project-Euler/Problem044.js diff --git a/Project-Euler/Problem044.js b/Project-Euler/Problem044.js new file mode 100644 index 0000000000..11dc989994 --- /dev/null +++ b/Project-Euler/Problem044.js @@ -0,0 +1,49 @@ +/** + * Problem 44 - Pentagon numbers + * + * @see {@link https://projecteuler.net/problem=44} + * + * Pentagonal numbers are generated by the formula, Pn=n(3nāˆ’1)/2. The first ten pentagonal numbers are: + * 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... + * It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 āˆ’ 22 = 48, is not pentagonal. + * Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = |Pk āˆ’ Pj| is minimised; what is the value of D? + * + * @author ddaniel27 + */ + +function problem44(){ + + let result = 0 + let searching = true + let k = 1 + + while (searching) { + k++ + let n = k * (3 * k - 1) / 2 //calculate Pk + + for (let j = k-1; j > 0; j--) { + let m = j * (3 * j - 1) / 2 //calculate all Pj < Pk + if (isPentagonal(n - m) && isPentagonal(n + m)) { //Check sum and difference + result = n-m + searching = false + break + } + } + } + + return result //Return D +} + +/** + * Function to check if a number is pentagonal or not + * This function solves n + * applying the solution for a quadratic function + * @see {@link https://en.wikipedia.org/wiki/Quadratic_function} + */ + +function isPentagonal(n) { + const pent = (Math.sqrt(24 * n + 1) + 1) / 6 + return pent === Math.floor(pent) +} + +export { problem44 } From 05c28cdde80d60e3cd2ca4fb3cae6bca89acd5bb Mon Sep 17 00:00:00 2001 From: ddaniel27 <67126972+ddaniel27@users.noreply.github.com> Date: Thu, 13 Oct 2022 21:19:38 -0500 Subject: [PATCH 2/5] [UPDATE] Code styling update --- Project-Euler/Problem044.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/Project-Euler/Problem044.js b/Project-Euler/Problem044.js index 11dc989994..f8b4a07fed 100644 --- a/Project-Euler/Problem044.js +++ b/Project-Euler/Problem044.js @@ -11,37 +11,36 @@ * @author ddaniel27 */ -function problem44(){ - +function problem44 () { let result = 0 let searching = true let k = 1 while (searching) { - k++ - let n = k * (3 * k - 1) / 2 //calculate Pk - - for (let j = k-1; j > 0; j--) { - let m = j * (3 * j - 1) / 2 //calculate all Pj < Pk - if (isPentagonal(n - m) && isPentagonal(n + m)) { //Check sum and difference - result = n-m - searching = false - break - } + k++ + const n = k * (3 * k - 1) / 2 // calculate Pk + + for (let j = k - 1; j > 0; j--) { + const m = j * (3 * j - 1) / 2 // calculate all Pj < Pk + if (isPentagonal(n - m) && isPentagonal(n + m)) { // Check sum and difference + result = n - m + searching = false + break } + } } - return result //Return D + return result // Return D } /** * Function to check if a number is pentagonal or not - * This function solves n + * This function solves n * applying the solution for a quadratic function * @see {@link https://en.wikipedia.org/wiki/Quadratic_function} */ -function isPentagonal(n) { +function isPentagonal (n) { const pent = (Math.sqrt(24 * n + 1) + 1) / 6 return pent === Math.floor(pent) } From 0d98f242e1bf2fee9f133960e5a3781cc4052035 Mon Sep 17 00:00:00 2001 From: Daniel Dorado Date: Fri, 14 Oct 2022 08:10:48 -0500 Subject: [PATCH 3/5] [UPDATE] Change return condition, added an input for main function, added tests for problem 44 --- Project-Euler/Problem044.js | 16 ++++++---------- Project-Euler/test/Problem044.test.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 Project-Euler/test/Problem044.test.js diff --git a/Project-Euler/Problem044.js b/Project-Euler/Problem044.js index f8b4a07fed..bec9d12a6d 100644 --- a/Project-Euler/Problem044.js +++ b/Project-Euler/Problem044.js @@ -11,26 +11,22 @@ * @author ddaniel27 */ -function problem44 () { - let result = 0 - let searching = true - let k = 1 +function problem44 (k) { + if( k < 1 ) { + throw new Error('Invalid Input') + } - while (searching) { + while (true) { k++ const n = k * (3 * k - 1) / 2 // calculate Pk for (let j = k - 1; j > 0; j--) { const m = j * (3 * j - 1) / 2 // calculate all Pj < Pk if (isPentagonal(n - m) && isPentagonal(n + m)) { // Check sum and difference - result = n - m - searching = false - break + return (n - m) // return D } } } - - return result // Return D } /** diff --git a/Project-Euler/test/Problem044.test.js b/Project-Euler/test/Problem044.test.js new file mode 100644 index 0000000000..bf6cccd60e --- /dev/null +++ b/Project-Euler/test/Problem044.test.js @@ -0,0 +1,18 @@ +import { problem44 } from '../Problem044.js' + +describe('checking nth prime number', () => { + it('should be invalid input if number is negative', () => { + expect(() => problem44(-3)).toThrowError('Invalid Input') + }) + it('should be invalid input if number is 0', () => { + expect(() => problem44(0)).toThrowError('Invalid Input') + }) + // Project Euler Condition Check + test('if the number is greater or equal to 1', () => { + expect(problem44(1)).toBe(5482660) + }) + // Project Euler Second Value for Condition Check + test('if the number is greater or equal to 2167', () => { + expect(problem44(2167)).toBe(8476206790) + }) +}) From 11318775a799b650474d5773b9eef6cc1cfadaf9 Mon Sep 17 00:00:00 2001 From: Daniel Dorado Date: Fri, 14 Oct 2022 08:41:56 -0500 Subject: [PATCH 4/5] [UPDATE] minor styling fixes to standard javascript --- Project-Euler/Problem044.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Project-Euler/Problem044.js b/Project-Euler/Problem044.js index bec9d12a6d..575a9271ad 100644 --- a/Project-Euler/Problem044.js +++ b/Project-Euler/Problem044.js @@ -12,7 +12,7 @@ */ function problem44 (k) { - if( k < 1 ) { + if (k < 1) { throw new Error('Invalid Input') } @@ -23,7 +23,7 @@ function problem44 (k) { for (let j = k - 1; j > 0; j--) { const m = j * (3 * j - 1) / 2 // calculate all Pj < Pk if (isPentagonal(n - m) && isPentagonal(n + m)) { // Check sum and difference - return (n - m) // return D + return (n - m) // return D } } } From bdce14528322defe7237a658bfd9244a4359b808 Mon Sep 17 00:00:00 2001 From: Daniel Dorado Date: Fri, 14 Oct 2022 11:29:14 -0500 Subject: [PATCH 5/5] [UPDATE] Fix parentheses in main function return --- Project-Euler/Problem044.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project-Euler/Problem044.js b/Project-Euler/Problem044.js index 575a9271ad..04d531d569 100644 --- a/Project-Euler/Problem044.js +++ b/Project-Euler/Problem044.js @@ -23,7 +23,7 @@ function problem44 (k) { for (let j = k - 1; j > 0; j--) { const m = j * (3 * j - 1) / 2 // calculate all Pj < Pk if (isPentagonal(n - m) && isPentagonal(n + m)) { // Check sum and difference - return (n - m) // return D + return n - m // return D } } }