From b7309f0018722801232f9de6219666e6ef69fd32 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Fri, 7 Oct 2022 20:36:47 +0530 Subject: [PATCH 1/6] =?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/6] =?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/6] =?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/6] =?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/6] =?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 29f39affba97d5e01d3f59ed25f4f58132d77b4f Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Tue, 18 Oct 2022 23:25:00 +0530 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Added=20test=20ca?= =?UTF-8?q?ses=20for=20Project=20Euler=20Problem-3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/test/Problem003.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Project-Euler/test/Problem003.test.js diff --git a/Project-Euler/test/Problem003.test.js b/Project-Euler/test/Problem003.test.js new file mode 100644 index 0000000000..0f6cf4e6da --- /dev/null +++ b/Project-Euler/test/Problem003.test.js @@ -0,0 +1,12 @@ +import { largestPrime } from '../Problem003.js' + +describe('Largest prime factor', () => { + test('if the number is 13195', () => { + expect(largestPrime(13195)).toBe(29) + }) + // Project Euler Condition Check + test('if the number is 600851475143', () => { + // Default value is same as the tested value + expect(largestPrime()).toBe(6857) + }) +}) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy