My Algorithm

First, a a standard prime sieve finds all prime numbers up to our limit (1000000 by default) and keeps them in a std::set.

Then each prime x in std::set is rotated by one digit to the right:
1. get the right-most digit:auto digit = rotated % 10;
2. move all digits by one digit to the right ("erasing" the right-most digit):rotated /= 10;
3. prepend the right-most digit: rotated += digit * shift;
4. check whether rotated is part of our std::set, too
5. if rotated is equal to our initial value x then we checked all rotations

The only point of interest is shift which is a power of 10 such that 10^a = shift <= x <= 10^{a+1}.
E.g., if x = 3456 then shift = 1000.

Note

There are a few options to speed up the code:
1. All prime numbers are odd (except for 2): if x != 2 and any digit is even then this prime can't be circular.
2. We can simplify point 1 by noting that all single-digit primes are circular.

Interactive test

You can submit your own input to my program and it will be instantly processed at my server:

Input data (separated by spaces or newlines):

This is equivalent toecho 100 | ./35

Output:

(please click 'Go !')

Note: the original problem's input 1000000cannot be enteredbecause just copying results is a soft skill reserved for idiots.

(this interactive test is still under development, computations will be aborted after one second)

My code

… was written in C++11 and can be compiled with G++, Clang++, Visual C++. You can download it, too.

The code contains #ifdefs to switch between the original problem and the Hackerrank version.Enable #ifdef ORIGINAL to produce the result for the original problem (default setting for most problems).

#include<iostream>

#include<set>

intmain()

{

// highest number (1000000 in original problem)

unsignedint n;

std::cin>> n;

// precomputation: find all prime numbers up to n

std::set<unsignedint> primes;

primes.insert(2);

for (unsignedint i = 3; i <= n; i += 2)

{

bool isPrime = true;

// test against all prime numbers we have so far (in ascending order)

for (auto x : primes)

{

// divisible => not prime

if (i % x == 0)

{

isPrime = false;

break;

}

// prime is too large to be a divisor

if (x*x > i)

break;

}

// yes, we have a prime

if (isPrime)

primes.insert(i);

}

// now look at all primes

unsignedint sum = 0;

for (auto x : primes)

{

// move the right-most digit to the front of the number

// we need to know the "position" of the front-most digit:

// shift will be 1 for x = 1..9

// shift will be 10 for x = 10..99

// shift will be 100 for x = 100..999 and so on

unsignedint shift = 1;

while (x > shift * 10)

shift *= 10;

auto rotated = x;

do

{

// take right-most digit

auto digit = rotated % 10;

// remove it

rotated /= 10;

// and prepend it

rotated += digit * shift;

// rotated number not prime ?

if (primes.count(rotated) == 0)

break;

} while (rotated != x); // finished the circle ? (we have the initial number again)

Changelog

Hackerrank

Difficulty

5%
Project Euler ranks this problem at 5% (out of 100%).

Hackerrank describes this problem as easy.

Note:Hackerrank has strict execution time limits (typically 2 seconds for C++ code) and often a much wider input range than the original problem.In my opinion, Hackerrank's modified problems are usually a lot harder to solve. As a rule thumb: brute-force is rarely an option.

Those links are just an unordered selection of source code I found with a semi-automatic search script on Google/Bing/GitHub/whatever.
You will probably stumble upon better solutions when searching on your own.
Maybe not all linked resources produce the correct result and/or exceed time/memory limits.

Heatmap

Please click on a problem's number to open my solution to that problem:

green

solutions solve the original Project Euler problem and have a perfect score of 100% at Hackerrank, too

yellow

solutions score less than 100% at Hackerrank (but still solve the original problem easily)

gray

problems are already solved but I haven't published my solution yet

blue

solutions are relevant for Project Euler only: there wasn't a Hackerrank version of it (at the time I solved it) or it differed too much

orange

problems are solved but exceed the time limit of one minute or the memory limit of 256 MByte

red

problems are not solved yet but I wrote a simulation to approximate the result or verified at least the given example - usually I sketched a few ideas, too

black

problems are solved but access to the solution is blocked for a few days until the next problem is published

[new]

the flashing problem is the one I solved most recently

I stopped working on Project Euler problems around the time they released 617.

The 310 solved problems (that's level 12) had an average difficulty of 32.6&percnt; at Project Euler and
I scored 13526 points (out of 15700 possible points, top rank was 17 out of &approx;60000 in August 2017)
at Hackerrank's Project Euler+.

My username at Project Euler is stephanbrumme while it's stbrumme at Hackerrank.

Copyright

I hope you enjoy my code and learn something - or give me feedback how I can improve my solutions.All of my solutions can be used for any purpose and I am in no way liable for any damages caused.You can even remove my name and claim it's yours. But then you shall burn in hell.

The problems and most of the problems' images were created by Project Euler.Thanks for all their endless effort !!!

more about me can be found on my homepage,
especially in my coding blog.
some names mentioned on this site may be trademarks of their respective owners.
thanks to the KaTeX team for their great typesetting library !