Ad
Code
Diff
  • function countCirculars(n) {
      let count = 0;
    
      for (let i=3;i<=n; i+=2) {
        if (isPrime(i) && isCirclePrime(i))
          count++;
      }
    
      return count+1;
    }
    
    function isPrime(n) {
      if (n <= 1)
        return false
      else if (n <= 3)
        return true
      else if ((n % 2) === 0 || n % 3 === 0)
        return false
    
      let i = 5;
    
      while (i*i <= n) {
        if (n % i === 0 || n % (i + 2) === 0)
          return false
        i = i + 6
      }
      return true
    }
    
    function isCirclePrime(n) {
      let strN = n.toString();
      for (let i = 1; i<strN.length; i++) {
        strN = shiftString(strN);
        if (!isPrime(parseInt(strN)))
          return false
      }
      return true;
    }
    
    function shiftString(str) {
      return str.substring(1) + str.substring(0,1);
    }
    
    • function countCirculars(n) {
    • let count = 0;
    • for (let i=3;i<=n; i+=2) {
    • if (isPrime(i) && isCirclePrime(i))
    • count++;
    • }
    • return count+1;
    • }
    • function isPrime(n) {
    • for (let i=2; i<=Math.sqrt(n); i++) {
    • if (n%i === 0)
    • return false;
    • if (n <= 1)
    • return false
    • else if (n <= 3)
    • return true
    • else if ((n % 2) === 0 || n % 3 === 0)
    • return false
    • let i = 5;
    • while (i*i <= n) {
    • if (n % i === 0 || n % (i + 2) === 0)
    • return false
    • i = i + 6
    • }
    • return true;
    • return true
    • }
    • function isCirclePrime(n) {
    • let strN = n.toString();
    • for (let i = 1; i<strN.length; i++) {
    • strN = shiftString(strN);
    • if (!isPrime(parseInt(strN)))
    • return false
    • }
    • return true;
    • }
    • function shiftString(str) {
    • return str.substring(1) + str.substring(0,1);
    • }

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?

function countCirculars(n) {
  let count = 0;

  for (let i=3;i<=n; i+=2) {
    if (isPrime(i) && isCirclePrime(i))
      count++;
  }

  return count+1;
}

function isPrime(n) {
  for (let i=2; i<=Math.sqrt(n); i++) {
    if (n%i === 0)
      return false;
  }
  return true;
}

function isCirclePrime(n) {
  let strN = n.toString();
  for (let i = 1; i<strN.length; i++) {
    strN = shiftString(strN);
    if (!isPrime(parseInt(strN)))
      return false
  }
  return true;
}

function shiftString(str) {
  return str.substring(1) + str.substring(0,1);
}