Primes

Find the largest six digit integer a b c d e f \overline{abcdef} such that all its 6 cyclic permutations of its digits is always a prime number .

For example, 197 is a member as all the three cyclic permutations 197, 971, and 719 are primes.


The answer is 999331.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Giorgio Coniglio
Jul 31, 2016

All p prime numbers with more than 2 digits ends in 1, 3, 7 or 9 and as such, our six digits number can only have one or more of those digits. So, if you start with 999999 which obviously is not a prime, you after some testing, will eventually end up with 999331 which is our answer.

These primes can be found here: https://oeis.org/A068652 (Cyclic permutation primes).

Salmaan Shahid
Jan 29, 2017

Here's my solution with C. Though the code is a bit long but it was fast to execute as I only checked the cases in which all the digits were odd

#include <stdio.h>
#include <stdbool.h>
bool isprime(long long int x);
int main(void)
{
  long long int a, b;
  int ar[6];
  for (a = 100000; a < 1000000; a++)
  {
    b = a;
    for (int i = 0; i < 6; i++)
    {
      ar[i] = b % 10;
      b /= 10;
    }

    for (int z = 0; z < 1; z++) 
    {
      if ((ar[0] % 2) == 0 || (ar[1] % 2) == 0 || (ar[2] % 2) == 0 || (ar[3] % 2) == 0 || (ar[4] % 2) == 0 || (ar[5] % 2) == 0)
    {
      break;
    }

    long long int kar[6];
    kar[0] = a;
    kar[1] = 100000 * ar[0] + 10000 * ar[5] + 1000 * ar[4] + 100 * ar[3] + 10 * ar[2] + ar[1];
    kar[2] = 100000 * ar[1] + 10000 * ar[0] + 1000 * ar[5] + 100 * ar[4] + 10 * ar[3] + ar[2];
    kar[3] = 100000 * ar[2] + 10000 * ar[1] + 1000 * ar[0] + 100 * ar[5] + 10 * ar[4] + ar[3];
    kar[4] = 100000 * ar[3] + 10000 * ar[2] + 1000 * ar[1] + 100 * ar[0] + 10 * ar[5] + ar[4];
    kar[5] = 100000 * ar[4] + 10000 * ar[3] + 1000 * ar[2] + 100 * ar[1] + 10 * ar[0] + ar[5];
    if (isprime(kar[0]) && isprime(kar[1]) && isprime(kar[2]) && isprime(kar[3]) && isprime(kar[4]) && isprime(kar[5]))
    {
      printf("%lld\n", a);
    }
    }
  }
}
bool isprime(long long int x)
{
  long long int i;
      for (i = 2; i < x; i++)
      {
        if ((x % i) == 0)
        {
          break;
        }
      }
      if (i == x)
      {
        return true;
      }
      else
      {
        return false;
      }
}
Nguyên Nguyễn
Dec 2, 2016

I'm beginner in Python , my code take a quite long time , but the result is correct .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from collections import deque

def isPrime(number):
    if (number<1):
        return False
    else:
        for i in range(2,int(math.sqrt(number))+1):
            if (number % i)==0:
                return False
    return True

problem_list=[]
for i in range(100000,1000000):
    if  (isPrime(i)):
        problem_list.append(i)

def checkPrime(number):
    count=0
    a=deque(str(number))
    for i in range(0,len(a)):
        tmp=int("".join(a))

        if (isPrime(tmp)):
            count=count+1
        else:
            return 0
        a.rotate(1)
    return    count

result_list=[]
for i in problem_list:
    if (checkPrime(i)!=0):
        result_list.append(i)

print(max(result_list))

I'm just a beginner at Java Programming, so the code is very very messy I believe, but here it is:

package javaapplication6;

public class JavaApplication6 {

private static boolean isPrime(Integer num) {

    if (num < 2) return false;
    if (num == 2) return true;
    if (num % 2 == 0) return false;
    for (int i = 3; i * i <= num; i += 2)
        if (num % i == 0) return false;
    return true;

}

public static void main(String[] args) {

  Integer temp;
  int countPrime=0;
  int flag=1;
  Integer j;
  Integer count;
  for(count = 100001;count<1000000;count+=2)
  {

      if(isPrime(count)!=true)
         continue;

      String str = count.toString();
      for(j=0;j<6;j++)
         {
            if(!(str.charAt(j)=='1'||str.charAt(j)=='3'||str.charAt(j)=='7'||str.charAt(j)=='9'))
               flag = 0;              
         }

     String newString = str;
      for(j=0;j<6;j++)
    {
        if(flag==0)
          break;

      newString = newString.substring(1,6)+newString.charAt(0);
      temp=Integer.parseInt(newString);

      if(isPrime(temp)==true)
          countPrime++;   
      else
          break;

    } 
   if(countPrime==6){
         System.out.println(count);
     }

countPrime= 0;      
 flag=1;  }

} }

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...