I have been trying to solve Project Euler problem number 35. The problem is to find:
How many circular primes are there below one million?
A circular prime is a prime where all the rotations of its digits are primes too. E.g. 197 is a circular prime as 197, 971, and 719 are all primes too.
Initially I was "brute-forcing" it by checking for divisors of the number up to its square root. However, I've realised how inefficient this was when the browser crashed above 10,000. Then I've tried using the Sieve of Eratosthenes, and now it can calculate the primes up to 1 million in ~4 seconds.
However, the part that is still to inefficient is the function to check if the primes are circular. I have made a few changes, but the browser still crashes above 100,000. What extra changes can I make?
//Erastosthenes sieve
function findPrimes(range) {
var numbers = [1];
var prime = 2;
var primesArr = [2]
//Stop searching once prime is as big as range
while(prime < range) {
//Fill var numbers all mulitipes of prime
for(var i = 1; i <= range && prime * i <= range ; ++i) {
numbers[prime * i - 1] = prime * i;
}
//Move onto the next prime
while(numbers[prime-1] !== undefined) {
++prime;
}
//Add current prime to the array
primesArr.push(prime);
}
//Delete the last prime which is bigger than range
primesArr.pop();
return primesArr;
}
//Find primes within range which are circular primes
function findCircularPrimes(range) {
//Generate array of primes from findPrimes array
var primes = findPrimes(range);
var circularPrimes = [];
//Loop through each of the primes
for(var i = 0; i < primes.length; ++i) {
//Split current prime into an array
var numArr = primes[i].toString().split('');
var truefalse = true;
//Cycle through each of the digits and check if its a prime (and therefore a member of primes)
for (var j = 0; j < numArr.length; ++j) {
numArr.push(numArr.shift());
//If current rotation is not a prime break the loop and 'return' false
if (primes.indexOf(Number(numArr.join(''))) === -1) {
truefalse = false;
break;
}
}
//If truefalse variable is true add circular prime to array
if(truefalse) {
circularPrimes.push(primes[i]);
}
}
return circularPrimes;
}
var then = Date.now();
var primes = findCircularPrimes(100000).length
alert(primes);