Do one thing and do it well. rotate() is overloaded.
Your isprime() takes O(n) (where n is the inputted number)
You need to traverse all the numbers O(n)
So the total complexity will be O(n^2). When n -> 1e6, that's quite big.
I deeply apologize. I clearly wasn't thinking about ruining it for someone else. And if you would, what are some of the limitations of my rotating algorithm?
If you wouldn't mind. I would like to add that I did get the right answer! It only took about 2 hours is all! Also, I am just learning programming so I am looking forward to the joys in the near future!
Hehe,, turns out my rotating code is EXACTLY the same as yours. Mine runs in 0.2 seconds.
But I will post it anyways. I've used Sieve of Eratosthenes to check primes
int length(int num)
{
int count=0;
while (num!=0)
{
count++;
num /= 10;
}
return count;
}
int isCircular(int P)
{
int L=length(P)-1;
if (L==0) returntrue;
for (int i=1;i<=L;i++)
{
P=P%10*pow(10,L)+P/10;
if (Sieve[P] != PRIME) return 0;
Sieve[P] = !PRIME;
}
return P;
}
Now that we are on a comical note, here is my first attempt at the rotating algorithm! I felt accomplished when I came up with the simpler version after more than an hour of thinking!
Negative! I have not yet made use of the so called Sieve of Eratosthenes. I took a break and moved on to Problem 36 instead. Which is equally if not more engaging I might add! :D
@mathematicsFanatic
I suggest you first solve this problem. You've got your rotating part right. So why not learn the Sieve. Its incredibly simple and exciting. I'm sure you will find it useful.
Currently, my rotate function takes a value and rotates the digits around. Let's say I want to rotate the number 1397. The function would print out 1397, 7139, 9713, and 3971.