One Four Two Eight Five Seven

We define an integer n n to be a cyclic prime if all its cyclic permutations are also prime numbers. Find the smallest cyclic prime number whose cyclic permutations are all larger than 500.

As an explicit example, 199933 is a cyclic prime because 199933, 999331, 993319, 933199, 331999, 319993 are all prime numbers.


The answer is 1193.

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.

3 solutions

Arjen Vreugdenhil
Nov 15, 2015

Since prime numbers do not end in an even digit of 5, the number must be made of 1, 3, 7, and 9 only. Also, all digits should be different (unless it is all ones and the number of digits is prime, as in 11111).

I consulted a table of prime numbers under 10,000.

The possible 3-digit candidates are 779 and 799. They didn't work.

For 4-digit candidates, I started at 1117 (but 1711 isn't prime); then tried 1193 \boxed{1193} , which worked out, since 1193, 1931, 3119, and 9311 are all prime.

Haha. This note makes this problem trivial!

Pi Han Goh - 5 years, 7 months ago
Arulx Z
Nov 12, 2015

I used trial division to test for primes because it's easier to implement (and the number was possibly small).

The method cyclic check if cyclic permutations of the number are prime and greater than 500. In the line

1
int(str(n % 10) + str(n / 10))

  • n % 10 returns the last digit of the number.
  • n / 10 removes the last digit of the number as Python automatically rounds down integer division.
  • str(x) + str(y) is used for concatenation of the numbers. Then int(z) converts the concatenated string back to int.

Here's my code -

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import math

def prime(n):
    if n & 1 == 0 and n > 2: # n & 1 isolates the last bit. Fastest way to check if n is divisible by 2 in binary
        return False
    return all(n % i for i in range(3, int(n ** 0.5), 2))

def cyclic(n):
    for x in xrange(int(math.log10(n)) + 1): # ceil(log10(n)). Returns the number of digits in a number
        if not n > 500 or not prime(n):
            return False
        n = int(str(n % 10) + str(n / 10))
    return True

n = 501

while not cyclic(n):
    n += 2 #primes greater than 2 will always be odd.

print n

Moderator note:

Yes, this code does what we want it to. Note that we can simplify it slightly by checking primes directly, instead of checking all odd numbers.

Bill Bell
Nov 15, 2015

No strings attached.

 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
36
from gmpy2 import is_prime, next_prime

def nextCyclic(m):
    def genIndex(start,modulo):
        i=start
        count=0
        while True:
            yield i
            count+=1
            if count>=modulo:
                break
            i+=1
            i=i%modulo

    digits=[]
    while m:
        digits.insert(0,m%10)
        m=m//10

    for s in range(1, len(digits)):
        accum=0
        for gen in genIndex(s,len(digits)):
            accum=10*accum+digits[gen]
        yield accum

n=499
while True:
    n=next_prime(n)
    for nc in nextCyclic(n):
        if nc<=500 or not is_prime(nc):
            break
    else:
        print '--->', n
        for nc in nextCyclic(n):
            print nc, is_prime(nc)
        break

Why did you use nested function?

Arulx Z - 5 years, 7 months ago

Log in to reply

This is a form of information hiding . Parts of the script that call nextCyclic do not need to know how it accomplishes its task; therefore, genIndex is hidden inside it, as a nested function.

Bill Bell - 5 years, 7 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...