Content-Length: 16751 | pFad | http://github.com/TheAlgorithms/JavaScript/pull/1570.patch
thub.com
From b7309f0018722801232f9de6219666e6ef69fd32 Mon Sep 17 00:00:00 2001
From: Omkarnath Parida
Date: Fri, 7 Oct 2022 20:36:47 +0530
Subject: [PATCH 01/10] =?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/10] =?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/10] =?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/10] =?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/10] =?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/10] =?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/10] =?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 c62216b1590561591e3e150af5a811d11c08efdc Mon Sep 17 00:00:00 2001
From: Omkarnath Parida
Date: Mon, 23 Oct 2023 18:04:37 +0530
Subject: [PATCH 08/10] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20added=20test=20?=
=?UTF-8?q?cases=20for=20project=20euler=20problem=206?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Project-Euler/test/Problem006.test.js | 11 +++++++++++
1 file changed, 11 insertions(+)
create mode 100644 Project-Euler/test/Problem006.test.js
diff --git a/Project-Euler/test/Problem006.test.js b/Project-Euler/test/Problem006.test.js
new file mode 100644
index 0000000000..1323a34ac2
--- /dev/null
+++ b/Project-Euler/test/Problem006.test.js
@@ -0,0 +1,11 @@
+import { squareDifference } from '../Problem006.js'
+
+describe('Square Difference', () => {
+ test('difference between the sum of the squares of the first ten natural numbers and the square of the sum', () => {
+ expect(squareDifference(10)).toBe(2640)
+ })
+ // Project Euler Condition Check
+ test('difference between the sum of the squares of the first one hundred natural numbers and the square of the sum', () => {
+ expect(squareDifference()).toBe(25164150)
+ })
+})
From 497782ba5171c43b21b09513c322e3fd38586668 Mon Sep 17 00:00:00 2001
From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Date: Mon, 23 Oct 2023 12:35:15 +0000
Subject: [PATCH 09/10] Updated Documentation in README.md
---
DIRECTORY.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/DIRECTORY.md b/DIRECTORY.md
index aea7b1548f..ce660a3975 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -166,6 +166,7 @@
* [Area](Maths/Area.js)
* [ArithmeticGeometricMean](Maths/ArithmeticGeometricMean.js)
* [ArmstrongNumber](Maths/ArmstrongNumber.js)
+ * [AutomorphicNumber](Maths/AutomorphicNumber.js)
* [AverageMean](Maths/AverageMean.js)
* [AverageMedian](Maths/AverageMedian.js)
* [BinaryConvert](Maths/BinaryConvert.js)
From ea720c89ba93a1ea50325401529c1531dd5c8de6 Mon Sep 17 00:00:00 2001
From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Date: Mon, 23 Oct 2023 12:35:15 +0000
Subject: [PATCH 10/10] Updated Documentation in README.md
---
DIRECTORY.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/DIRECTORY.md b/DIRECTORY.md
index aea7b1548f..ce660a3975 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -166,6 +166,7 @@
* [Area](Maths/Area.js)
* [ArithmeticGeometricMean](Maths/ArithmeticGeometricMean.js)
* [ArmstrongNumber](Maths/ArmstrongNumber.js)
+ * [AutomorphicNumber](Maths/AutomorphicNumber.js)
* [AverageMean](Maths/AverageMean.js)
* [AverageMedian](Maths/AverageMedian.js)
* [BinaryConvert](Maths/BinaryConvert.js)
--- a PPN by Garber Painting Akron. With Image Size Reduction included!Fetched URL: http://github.com/TheAlgorithms/JavaScript/pull/1570.patch
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy