From b7309f0018722801232f9de6219666e6ef69fd32 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Fri, 7 Oct 2022 20:36:47 +0530 Subject: [PATCH 1/8] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20Added=20solution=20fo?= =?UTF-8?q?r=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 2/8] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Spelling=20mistake=20?= =?UTF-8?q?fixes?= 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 3/8] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20changed=20variabl?= =?UTF-8?q?e=20name=20from=20`inc`=20to=20`candidateValue`=20and=20thrown?= =?UTF-8?q?=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 4/8] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Modified=20the=20?= =?UTF-8?q?code?= 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 5/8] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Added=20test=20ca?= =?UTF-8?q?se=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 ecbb0651c00d8fc00030fac7b53a68693502133e Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 16 Oct 2022 19:28:51 +0530 Subject: [PATCH 6/8] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Added=20testcases?= =?UTF-8?q?=20for=20Project=20Euler=20Problem=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem002.js | 6 ++++-- Project-Euler/test/Problem002.test.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 Project-Euler/test/Problem002.test.js diff --git a/Project-Euler/Problem002.js b/Project-Euler/Problem002.js index 5b8c3ddd51..8acfc1532e 100644 --- a/Project-Euler/Problem002.js +++ b/Project-Euler/Problem002.js @@ -5,8 +5,10 @@ const PHI = (1 + SQ5) / 2 // definition of PHI // theoretically it should take O(1) constant amount of time as long // arithmetic calculations are considered to be in constant amount of time export const EvenFibonacci = (limit) => { + if (limit < 1) throw new Error('Fibonacci sequence limit can\'t be lesser than 1') + const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI)) const n = Math.floor(highestIndex / 3) - return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) - - ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5 + return Math.floor(((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) - + ((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5) } diff --git a/Project-Euler/test/Problem002.test.js b/Project-Euler/test/Problem002.test.js new file mode 100644 index 0000000000..388b61a621 --- /dev/null +++ b/Project-Euler/test/Problem002.test.js @@ -0,0 +1,14 @@ +import { EvenFibonacci } from '../Problem002' + +describe('Even Fibonacci numbers', () => { + it('should throw error when limit is less than 1', () => { + expect(() => EvenFibonacci(-1)).toThrowError('Fibonacci sequence limit can\'t be lesser than 1') + }) + test('when limit is greater than 0', () => { + expect(EvenFibonacci(40)).toBe(44) + }) + // Project Euler Condition Check + test('when limit is 4 million', () => { + expect(EvenFibonacci(4e6)).toBe(4613732) + }) +}) From d39e77f5713214577b63e7ae215c804e0291bf18 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 16 Oct 2022 14:10:37 +0000 Subject: [PATCH 7/8] Updated Documentation in README.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index dc439d495c..5f668918c9 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -268,6 +268,7 @@ * **Sorts** * [AlphaNumericalSort](Sorts/AlphaNumericalSort.js) * [BeadSort](Sorts/BeadSort.js) + * [BinaryInsertionSort](Sorts/BinaryInsertionSort.js) * [BogoSort](Sorts/BogoSort.js) * [BubbleSort](Sorts/BubbleSort.js) * [BucketSort](Sorts/BucketSort.js) From c8287867b78dc6184b44e0baff997064e88a7239 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sun, 16 Oct 2022 20:31:12 +0530 Subject: [PATCH 8/8] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Fixed=20grammer=20mis?= =?UTF-8?q?take?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem002.js | 2 +- Project-Euler/test/Problem002.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Project-Euler/Problem002.js b/Project-Euler/Problem002.js index 8acfc1532e..065a3a9f63 100644 --- a/Project-Euler/Problem002.js +++ b/Project-Euler/Problem002.js @@ -5,7 +5,7 @@ const PHI = (1 + SQ5) / 2 // definition of PHI // theoretically it should take O(1) constant amount of time as long // arithmetic calculations are considered to be in constant amount of time export const EvenFibonacci = (limit) => { - if (limit < 1) throw new Error('Fibonacci sequence limit can\'t be lesser than 1') + if (limit < 1) throw new Error('Fibonacci sequence limit can\'t be less than 1') const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI)) const n = Math.floor(highestIndex / 3) diff --git a/Project-Euler/test/Problem002.test.js b/Project-Euler/test/Problem002.test.js index 388b61a621..bccd770b4c 100644 --- a/Project-Euler/test/Problem002.test.js +++ b/Project-Euler/test/Problem002.test.js @@ -2,7 +2,7 @@ import { EvenFibonacci } from '../Problem002' describe('Even Fibonacci numbers', () => { it('should throw error when limit is less than 1', () => { - expect(() => EvenFibonacci(-1)).toThrowError('Fibonacci sequence limit can\'t be lesser than 1') + expect(() => EvenFibonacci(-1)).toThrowError('Fibonacci sequence limit can\'t be less than 1') }) test('when limit is greater than 0', () => { expect(EvenFibonacci(40)).toBe(44)