mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-19 10:05:41 +08:00

* [CREATE] Problem 28 solution for Project Euler * [UPDATE] Added an explanation for the formula used in the algorithm * [CREATE] Added Problem 35 for Project-Euler * [UPDATE] Little typo in the error string * [UPDATE] Some algorithm changes * [UPDATE] Fix test string * [UPDATE] Change prime numbers generator to import a standard sieve algorithm. * [UPDATE] Change sieve algorithm implementation and now the solution works well. Also added some optimizations
35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
/**
|
|
* 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 }
|