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 53e3938b0a29f0ee101b06c559d4f358cd7a0cd6 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Mon, 23 Oct 2023 00:32:56 +0530 Subject: [PATCH 06/12] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Added=20test=20?= =?UTF-8?q?cases=20for=20Project=20Euler=20Problem=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/test/Problem004.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Project-Euler/test/Problem004.test.js diff --git a/Project-Euler/test/Problem004.test.js b/Project-Euler/test/Problem004.test.js new file mode 100644 index 0000000000..cd6a55ac98 --- /dev/null +++ b/Project-Euler/test/Problem004.test.js @@ -0,0 +1,11 @@ +import { largestPalindromic } from '../Problem004.js' + +describe('Largest Palindromic Number', () => { + test('if digit is 2', () => { + expect(largestPalindromic(2)).toBe(9009) + }) + // Project Euler Condition Check + test('if digit is 3', () => { + expect(largestPalindromic(3)).toBe(906609) + }) +}) From 96224e7950ab7bde4799d8103523b779e523e58c Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Mon, 23 Oct 2023 00:34:58 +0530 Subject: [PATCH 07/12] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20auto=20prettier?= =?UTF-8?q?=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bit-Manipulation/BinaryCountSetBits.js | 2 +- Maths/AutomorphicNumber.js | 2 +- Maths/test/AutomorphicNumber.test.js | 16 ++++----- Project-Euler/Problem006.js | 6 ++-- Recursive/test/BinaryEquivalent.test.js | 48 ++++++++++++------------- Search/InterpolationSearch.js | 2 +- 6 files changed, 38 insertions(+), 38 deletions(-) diff --git a/Bit-Manipulation/BinaryCountSetBits.js b/Bit-Manipulation/BinaryCountSetBits.js index b879f3bd67..b959caf062 100644 --- a/Bit-Manipulation/BinaryCountSetBits.js +++ b/Bit-Manipulation/BinaryCountSetBits.js @@ -16,7 +16,7 @@ function BinaryCountSetBits(a) { let count = 0 while (a) { - a &= (a - 1) + a &= a - 1 count++ } diff --git a/Maths/AutomorphicNumber.js b/Maths/AutomorphicNumber.js index d1b6608316..ba008271fc 100644 --- a/Maths/AutomorphicNumber.js +++ b/Maths/AutomorphicNumber.js @@ -35,6 +35,6 @@ export const isAutomorphic = (n) => { n = Math.floor(n / 10) n_sq = Math.floor(n_sq / 10) } - + return true } diff --git a/Maths/test/AutomorphicNumber.test.js b/Maths/test/AutomorphicNumber.test.js index 19b963388c..57f40d27ee 100644 --- a/Maths/test/AutomorphicNumber.test.js +++ b/Maths/test/AutomorphicNumber.test.js @@ -9,19 +9,19 @@ describe('AutomorphicNumber', () => { }) test.each([ - { n: -3 , expected: false }, - { n: -25 , expected: false }, + { n: -3, expected: false }, + { n: -25, expected: false } ])('should return false when n is negetive', ({ n, expected }) => { expect(isAutomorphic(n)).toBe(false) }) test.each([ - { n: 7 , expected: false }, - { n: 83 , expected: false }, - { n: 0 , expected: true }, - { n: 1 , expected: true }, - { n: 376 , expected: true }, - { n: 90625 , expected: true }, + { n: 7, expected: false }, + { n: 83, expected: false }, + { n: 0, expected: true }, + { n: 1, expected: true }, + { n: 376, expected: true }, + { n: 90625, expected: true } ])('should return $expected when n is $n', ({ n, expected }) => { expect(isAutomorphic(n)).toBe(expected) }) diff --git a/Project-Euler/Problem006.js b/Project-Euler/Problem006.js index 474de2ae96..804b165558 100644 --- a/Project-Euler/Problem006.js +++ b/Project-Euler/Problem006.js @@ -1,8 +1,8 @@ // https://projecteuler.net/problem=6 export const squareDifference = (num = 100) => { - let sumOfSquares = (num)*(num+1)*(2*num+1)/6 - let sums = (num*(num+1))/2 - + let sumOfSquares = (num * (num + 1) * (2 * num + 1)) / 6 + let sums = (num * (num + 1)) / 2 + return sums ** 2 - sumOfSquares // difference of square of the total sum and sum of squares } diff --git a/Recursive/test/BinaryEquivalent.test.js b/Recursive/test/BinaryEquivalent.test.js index b79a455eed..ddabb7d477 100644 --- a/Recursive/test/BinaryEquivalent.test.js +++ b/Recursive/test/BinaryEquivalent.test.js @@ -1,29 +1,29 @@ -import { binaryEquivalent } from "../BinaryEquivalent"; +import { binaryEquivalent } from '../BinaryEquivalent' const tests = [ - { - test: 2, - expectedValue: "10" - }, - { - test: 0, - expectedValue: "0" - }, - { - test: 543, - expectedValue: "1000011111" - }, - { - test: 4697621023, - expectedValue: "100011000000000000000001000011111" - } + { + test: 2, + expectedValue: '10' + }, + { + test: 0, + expectedValue: '0' + }, + { + test: 543, + expectedValue: '1000011111' + }, + { + test: 4697621023, + expectedValue: '100011000000000000000001000011111' + } ] -describe("Binary Equivalent", () => { - test.each(tests)( - "of $test should be $expectedValue", - ({test, expectedValue}) => { - expect(binaryEquivalent(test)).toBe(expectedValue); - } - ) +describe('Binary Equivalent', () => { + test.each(tests)( + 'of $test should be $expectedValue', + ({ test, expectedValue }) => { + expect(binaryEquivalent(test)).toBe(expectedValue) + } + ) }) diff --git a/Search/InterpolationSearch.js b/Search/InterpolationSearch.js index e6deae496f..93f3b78b0e 100644 --- a/Search/InterpolationSearch.js +++ b/Search/InterpolationSearch.js @@ -36,4 +36,4 @@ export function interpolationSearch(arr, key) { } return -1 -} \ No newline at end of file +} From 2863fa120279e201296f86f9c66390fac96a5bd7 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sat, 28 Sep 2024 13:23:25 +0530 Subject: [PATCH 08/12] =?UTF-8?q?=F0=9F=93=A6=20NEW:=20Project=20Euler=20P?= =?UTF-8?q?roblem=205=20improvement=20and=20added=20test=20cases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem005.js | 11 ++++++----- Project-Euler/test/Problem005.test.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 Project-Euler/test/Problem005.test.js diff --git a/Project-Euler/Problem005.js b/Project-Euler/Problem005.js index fe8901c94c..ddc5455572 100644 --- a/Project-Euler/Problem005.js +++ b/Project-Euler/Problem005.js @@ -5,11 +5,12 @@ Smallest multiple What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? */ -export const findSmallestMultiple = () => { - const divisors = [ - 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 - ] - let num = 21 +export const findSmallestMultiple = (maxDivisor) => { + if (isNaN(maxDivisor)) { + return 0 + } + const divisors = Array.from({ length: maxDivisor }, (_, i) => i + 1) + let num = maxDivisor + 1 let result while (!result) { diff --git a/Project-Euler/test/Problem005.test.js b/Project-Euler/test/Problem005.test.js new file mode 100644 index 0000000000..b7de7883ad --- /dev/null +++ b/Project-Euler/test/Problem005.test.js @@ -0,0 +1,14 @@ +import { findSmallestMultiple } from '../Problem005.js' + +describe.concurrent('Find smallest multiple', () => { + test('if max divisor is 10', () => { + expect(findSmallestMultiple(10)).toBe(2520) + }) + test('if max divisor is 15', () => { + expect(findSmallestMultiple(15)).toBe(360360) + }) + // Project Euler Condition Check + test('if max divisor is 20', () => { + expect(findSmallestMultiple(20)).toBe(232792560) + }) +}) From 96735d205fba18e310d0693f29ad739585ac1ef4 Mon Sep 17 00:00:00 2001 From: pomkarnath98 Date: Sat, 28 Sep 2024 08:05:19 +0000 Subject: [PATCH 09/12] Updated Documentation in README.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6c1fa7aeb8..7f6484cae5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -35,6 +35,8 @@ * [ROT13](Ciphers/ROT13.js) * [VigenereCipher](Ciphers/VigenereCipher.js) * [XORCipher](Ciphers/XORCipher.js) +* **Compression** + * [RLE](Compression/RLE.js) * **Conversions** * [ArbitraryBase](Conversions/ArbitraryBase.js) * [ArrayBufferToBase64](Conversions/ArrayBufferToBase64.js) @@ -285,6 +287,7 @@ * [Problem016](Project-Euler/Problem016.js) * [Problem017](Project-Euler/Problem017.js) * [Problem018](Project-Euler/Problem018.js) + * [Problem019](Project-Euler/Problem019.js) * [Problem020](Project-Euler/Problem020.js) * [Problem021](Project-Euler/Problem021.js) * [Problem023](Project-Euler/Problem023.js) From b58c0a78d23d4766575c48d5c0c3b9d7b22e8cfb Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Sat, 28 Sep 2024 13:40:00 +0530 Subject: [PATCH 10/12] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20code=20improve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/test/Problem005.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Project-Euler/test/Problem005.test.js b/Project-Euler/test/Problem005.test.js index b7de7883ad..bef74a5f26 100644 --- a/Project-Euler/test/Problem005.test.js +++ b/Project-Euler/test/Problem005.test.js @@ -7,6 +7,7 @@ describe.concurrent('Find smallest multiple', () => { test('if max divisor is 15', () => { expect(findSmallestMultiple(15)).toBe(360360) }) + // Project Euler Condition Check test('if max divisor is 20', () => { expect(findSmallestMultiple(20)).toBe(232792560) From 5f7d04675be9131918342126946786cb7a5e9773 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Thu, 3 Oct 2024 22:57:17 +0530 Subject: [PATCH 11/12] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20remove=20extra=20co?= =?UTF-8?q?de?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/Problem005.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/Project-Euler/Problem005.js b/Project-Euler/Problem005.js index ddc5455572..92f3df9b68 100644 --- a/Project-Euler/Problem005.js +++ b/Project-Euler/Problem005.js @@ -6,9 +6,6 @@ What is the smallest positive number that is evenly divisible by all of the numb */ export const findSmallestMultiple = (maxDivisor) => { - if (isNaN(maxDivisor)) { - return 0 - } const divisors = Array.from({ length: maxDivisor }, (_, i) => i + 1) let num = maxDivisor + 1 let result From a1117ef7789f21b6a9490c35d89ddbd370babe39 Mon Sep 17 00:00:00 2001 From: Omkarnath Parida Date: Tue, 8 Oct 2024 18:27:17 +0530 Subject: [PATCH 12/12] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20test=20cases=20?= =?UTF-8?q?writing=20imrovements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Project-Euler/test/Problem005.test.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Project-Euler/test/Problem005.test.js b/Project-Euler/test/Problem005.test.js index bef74a5f26..f3ac1b03d8 100644 --- a/Project-Euler/test/Problem005.test.js +++ b/Project-Euler/test/Problem005.test.js @@ -1,15 +1,12 @@ +import { expect } from 'vitest' import { findSmallestMultiple } from '../Problem005.js' describe.concurrent('Find smallest multiple', () => { - test('if max divisor is 10', () => { - expect(findSmallestMultiple(10)).toBe(2520) - }) - test('if max divisor is 15', () => { - expect(findSmallestMultiple(15)).toBe(360360) - }) - - // Project Euler Condition Check - test('if max divisor is 20', () => { - expect(findSmallestMultiple(20)).toBe(232792560) + test.each([ + [10, 2520], + [15, 360360], + [20, 232792560] + ])('max divisor -> %i, smallest multiple -> %i', (a, expected) => { + expect(findSmallestMultiple(a)).toBe(expected) }) }) 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