fix: Project Euler P35 off-by-one error (#1238)

* fix: Project Euler P35 off-by-one error

* Updated Documentation in README.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Lars Müller
2022-10-28 18:51:54 +02:00
committed by GitHub
parent 5364a1c31b
commit 0fab492ceb
2 changed files with 13 additions and 3 deletions

View File

@ -57,6 +57,7 @@
* [LocalMaximomPoint](Data-Structures/Array/LocalMaximomPoint.js)
* [NumberOfLocalMaximumPoints](Data-Structures/Array/NumberOfLocalMaximumPoints.js)
* [QuickSelect](Data-Structures/Array/QuickSelect.js)
* [Reverse](Data-Structures/Array/Reverse.js)
* **Graph**
* [Graph](Data-Structures/Graph/Graph.js)
* [Graph2](Data-Structures/Graph/Graph2.js)
@ -81,6 +82,7 @@
* **Tree**
* [AVLTree](Data-Structures/Tree/AVLTree.js)
* [BinarySearchTree](Data-Structures/Tree/BinarySearchTree.js)
* [SegmentTree](Data-Structures/Tree/SegmentTree.js)
* [Trie](Data-Structures/Tree/Trie.js)
* **Vectors**
* [Vector2](Data-Structures/Vectors/Vector2.js)
@ -105,18 +107,19 @@
* [RodCutting](Dynamic-Programming/RodCutting.js)
* [Shuf](Dynamic-Programming/Shuf.js)
* [SieveOfEratosthenes](Dynamic-Programming/SieveOfEratosthenes.js)
* [UniquePaths](Dynamic-Programming/UniquePaths.js)
* **Sliding-Window**
* [LongestSubstringWithoutRepeatingCharacters](Dynamic-Programming/Sliding-Window/LongestSubstringWithoutRepeatingCharacters.js)
* [PermutationinString](Dynamic-Programming/Sliding-Window/PermutationinString.js)
* [SudokuSolver](Dynamic-Programming/SudokuSolver.js)
* [TrappingRainWater](Dynamic-Programming/TrappingRainWater.js)
* [TribonacciNumber](Dynamic-Programming/TribonacciNumber.js)
* [UniquePaths](Dynamic-Programming/UniquePaths.js)
* [ZeroOneKnapsack](Dynamic-Programming/ZeroOneKnapsack.js)
* **Geometry**
* [ConvexHullGraham](Geometry/ConvexHullGraham.js)
* **Graphs**
* [BellmanFord](Graphs/BellmanFord.js)
* [BinaryLifting](Graphs/BinaryLifting.js)
* [BreadthFirstSearch](Graphs/BreadthFirstSearch.js)
* [BreadthFirstShortestPath](Graphs/BreadthFirstShortestPath.js)
* [ConnectedComponents](Graphs/ConnectedComponents.js)
@ -126,6 +129,7 @@
* [Dijkstra](Graphs/Dijkstra.js)
* [DijkstraSmallestPath](Graphs/DijkstraSmallestPath.js)
* [FloydWarshall](Graphs/FloydWarshall.js)
* [Kosaraju](Graphs/Kosaraju.js)
* [KruskalMST](Graphs/KruskalMST.js)
* [NodeNeighbors](Graphs/NodeNeighbors.js)
* [NumberOfIslands](Graphs/NumberOfIslands.js)
@ -158,6 +162,7 @@
* [EulerMethod](Maths/EulerMethod.js)
* [EulersTotient](Maths/EulersTotient.js)
* [EulersTotientFunction](Maths/EulersTotientFunction.js)
* [ExponentialFunction](Maths/ExponentialFunction.js)
* [ExtendedEuclideanGCD](Maths/ExtendedEuclideanGCD.js)
* [Factorial](Maths/Factorial.js)
* [Factors](Maths/Factors.js)
@ -190,6 +195,7 @@
* [MeanSquareError](Maths/MeanSquareError.js)
* [MidpointIntegration](Maths/MidpointIntegration.js)
* [MobiusFunction](Maths/MobiusFunction.js)
* [ModularArithmetic](Maths/ModularArithmetic.js)
* [ModularBinaryExponentiationRecursive](Maths/ModularBinaryExponentiationRecursive.js)
* [NumberOfDigits](Maths/NumberOfDigits.js)
* [Palindrome](Maths/Palindrome.js)
@ -209,6 +215,7 @@
* [ReversePolishNotation](Maths/ReversePolishNotation.js)
* [ShorsAlgorithm](Maths/ShorsAlgorithm.js)
* [SieveOfEratosthenes](Maths/SieveOfEratosthenes.js)
* [SieveOfEratosthenesIntArray](Maths/SieveOfEratosthenesIntArray.js)
* [SimpsonIntegration](Maths/SimpsonIntegration.js)
* [Softmax](Maths/Softmax.js)
* [SquareRoot](Maths/SquareRoot.js)
@ -243,6 +250,7 @@
* [Problem023](Project-Euler/Problem023.js)
* [Problem025](Project-Euler/Problem025.js)
* [Problem028](Project-Euler/Problem028.js)
* [Problem035](Project-Euler/Problem035.js)
* [Problem044](Project-Euler/Problem044.js)
* **Recursive**
* [BinaryEquivalent](Recursive/BinaryEquivalent.js)
@ -317,6 +325,7 @@
* [CheckRearrangePalindrome](String/CheckRearrangePalindrome.js)
* [CheckSnakeCase](String/CheckSnakeCase.js)
* [CheckWordOccurrence](String/CheckWordOccurrence.js)
* [CountLetters](String/CountLetters.js)
* [CountSubstrings](String/CountSubstrings.js)
* [CountVowels](String/CountVowels.js)
* [CreatePermutations](String/CreatePermutations.js)

View File

@ -15,7 +15,8 @@ function problem35 (n) {
if (n < 2) {
throw new Error('Invalid input')
}
const list = sieveOfEratosthenes(n).filter(prime => !prime.toString().match(/[024568]/)) // Get a list of primes without 0, 2, 4, 5, 6, 8
// Get a list of primes without 0, 2, 4, 5, 6, 8; this discards the circular primes 2 & 5
const list = sieveOfEratosthenes(n).filter(prime => !prime.toString().match(/[024568]/))
const result = list.filter((number, _idx, arr) => {
const str = String(number)
@ -28,7 +29,7 @@ function problem35 (n) {
return true // If all rotations are prime, then the number is circular prime
})
return result.length + 1 // Add 2 to the result because 2 is a circular prime
return result.length + 2 // Add 2 to the result because the circular primes 2 & 5 were discarded
}
export { problem35 }