-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
solution: Project Euler 35 #1201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3866e8b
[CREATE] Problem 28 solution for Project Euler
ddaniel27 3418cdd
[UPDATE] Added an explanation for the formula used in the algorithm
ddaniel27 d76604d
Merge branch 'TheAlgorithms:master' into master
ddaniel27 88fce40
[CREATE] Added Problem 35 for Project-Euler
ddaniel27 58d370a
[UPDATE] Little typo in the error string
ddaniel27 f5f8d36
[UPDATE] Some algorithm changes
ddaniel27 b7776bd
[UPDATE] Fix test string
ddaniel27 aa5f9db
[UPDATE] Change prime numbers generator to import a standard sieve
ddaniel27 e88994f
Merge branch 'TheAlgorithms:master' into master
ddaniel27 b94e5f9
[UPDATE] Change sieve algorithm implementation and now the solution
ddaniel27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Problem 35 - Circular primes | ||
* | ||
* @see {@link https://projecteuler.net/problem=35} | ||
* | ||
* The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. | ||
* There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. | ||
* How many circular primes are there below one million? | ||
* | ||
* @author ddaniel27 | ||
*/ | ||
import { sieveOfEratosthenes } from '../Maths/SieveOfEratosthenesIntArray' | ||
|
||
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 | ||
|
||
const result = list.filter((number, _idx, arr) => { | ||
const str = String(number) | ||
for (let i = 0; i < str.length; i++) { // Get all rotations of the number | ||
const rotation = str.slice(i) + str.slice(0, i) | ||
if (!arr.includes(Number(rotation))) { // Check if the rotation is prime | ||
return false | ||
} | ||
} | ||
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 | ||
} | ||
|
||
export { problem35 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { problem35 } from '../Problem035.js' | ||
|
||
describe('checking circular primes', () => { | ||
it('should be invalid input if number is negative', () => { | ||
expect(() => problem35(-3)).toThrowError('Invalid input') | ||
}) | ||
it('should be invalid input if number is 0', () => { | ||
expect(() => problem35(0)).toThrowError('Invalid input') | ||
}) | ||
// Project Euler Condition Check | ||
test('if the number is equal to 100 result should be 13', () => { | ||
expect(problem35(100)).toBe(13) | ||
}) | ||
// Project Euler Challenge Check | ||
test('if the number is equal to one million result should be 55', () => { | ||
expect(problem35(1000000)).toBe(55) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.