Files
JavaScript/Project-Euler/Problem035.js
Roland Hummel 86d333ee94 feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest

* chore: migrate to vitest mock functions

* chore: code style (switch to prettier)

* test: re-enable long-running test

Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime!

see #1193

* chore: code style

* chore: fix failing tests

* Updated Documentation in README.md

* Update contribution guidelines to state usage of Prettier

* fix: set prettier printWidth back to 80

* chore: apply updated code style automatically

* fix: set prettier line endings to lf again

* chore: apply updated code style automatically

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
2023-10-04 02:38:19 +05:30

40 lines
1.3 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')
}
// 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)
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 + 2 // Add 2 to the result because the circular primes 2 & 5 were discarded
}
export { problem35 }