Circular Prime

Author

Eric W. Weisstein
April 29, 2014

This notebook downloaded from http://mathworld.wolfram.com/notebooks/PrimeNumbers/CircularPrime.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/CircularPrime.html.

©2014 Wolfram Research, Inc. except for portions noted otherwise

Code

CircularPrimeQ[n_Integer]:=Block[{d=IntegerDigits[n]},
Which[
n<10,PrimeQ[n],
Intersection[d,{0,2,4,5,6,8}]==={},PrimeQ[FromDigits[#]]&/@And@@NestList[RotateRight,d,Length[d]-1],
True,False
]
]

Compute

Select[Range[10^4],CircularPrimeQ]//Timing

Select[Range[10^5],CircularPrimeQ]//Timing

Select[Range[10^6],CircularPrimeQ]//Timing

Select[Range[10^7],CircularPrimeQ]//Timing

Select[Range[10^7],CircularPrimeQ]//Timing

Select[Range[10^8],CircularPrimeQ]//Timing

Preprocess by sorting

SmallestCircularPermutation[n_Integer]:=Block[{d=IntegerDigits[n]},
Min[FromDigits/@NestList[RotateRight,d,Length[d]-1]]]

SetAttributes[SmallestCircularPermutation,Listable]

Select[Union[SmallestCircularPermutation[Range[10^5]]],CircularPrimeQ]//Timing

Select[Union[SmallestCircularPermutation[Range[10^6]]],CircularPrimeQ]//Timing