Definitions
A circular prime is a prime number with the property that the number generated at each intermediate step when cyclically permuting its (base 10) digits will also be prime.

For example:
1193 is a circular prime, since 1931, 9311 and 3119 are all also prime.

Note that a number which is a cyclic permutation of a smaller circular prime is not considered to be itself a circular prime. So 13 is a circular prime, but 31 is not.



A repunit (denoted by R) is a number whose base 10 representation contains only the digit 1.

For example:
R(2) = 11 and R(5) = 11111 are repunits.



Task
 Find the first 19 circular primes.


 If your language has access to arbitrary precision integer arithmetic, given that they are all repunits, find the next 4 circular primes.


 (Stretch) Determine which of the following repunits are probably circular primes: R(5003), R(9887), R(15073), R(25031), R(35317) and R(49081). The larger ones may take a long time to process so just do as many as you reasonably can.


See also
 Wikipedia article - Circular primes.
 Wikipedia article - Repunit.
 OEIS sequence A016114 - Circular primes.


#define floor(x) ((x*2.0-0.5)Shr 1)
 
Function isPrime(Byval p As Integer) As Boolean
    If p < 2 Then Return False
    If p Mod 2 = 0 Then Return p = 2
    If p Mod 3 = 0 Then Return p = 3
    Dim As Integer d = 5
    While d * d <= p
        If p Mod d = 0 Then Return False Else d += 2
        If p Mod d = 0 Then Return False Else d += 4
    Wend 
    Return True
End Function
 
Function isCircularPrime(Byval p As Integer) As Boolean
    Dim As Integer n = floor(Log(p)/Log(10))
    Dim As Integer m = 10^n, q = p
    For i As Integer = 0 To n
        If (q < p Or Not isPrime(q)) Then Return false
        q = (q Mod m) * 10 + floor(q / m)
    Next i
    Return true
End Function
 
Dim As Integer p = 2, dp = 1, cont = 0
Print("Primeros 19 primos circulares:")
While cont < 19
    If isCircularPrime(p) Then Print p;" "; : cont += 1
    p += dp: dp = 2
Wend
Sleep