# Thursday Code Puzzler: Circular Primes

It's Thursday, so it's time for another code puzzler. The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you find suitable.

*Note: Even though there really is nothing stopping you from finding a solution to this on the internet, try to keep honest, and come up with your own answer. It's all about the participation!*

**Circular Primes **

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 be prime. For example 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime.

Your task is to calculate how many circular primes exist below 1 million.

Catch up on all our previous puzzlers here

## Comments

## sun east replied on Thu, 2013/06/06 - 8:45am

A four lines solution in Scala:

## K Hyland replied on Thu, 2013/06/06 - 9:03am

Verbose Python solution:

Comments and improvements welcome.

## Ondřej Smola replied on Mon, 2013/06/17 - 10:18am in response to: K Hyland

## Bruce Cox replied on Sat, 2013/06/15 - 10:18am

@K Hyland

Your method for is finding all the primes is rather slow. You could use the Sieve of Eratosthenes to speed that part up and store the primes in a dict so you can easily test for primality without dividing again by lots of numbers. All numbers ending in 2, 4, 5, 6 or 8 are prime, except 2 and 5, so you could filter out any 2+-digit prime containing any of those numbers (that cuts the primes in consideration from 78000+ to 1113). That's probably not necessary when the primes are already in a dict.

Cheers,

Bruce

## Camilo Rivera replied on Thu, 2013/10/31 - 1:05am

Mi contribution in Ruby. Probably needs some tuning but here it goes anyway: