I am relatively new to the programming world so I apologize if my code seems naive in any sort of way.I have been making a piece of code that counts all the circular primes below a given number, 'input'. If you wonder what circular primes are, you can check it out here.
What i know:
The code runs but fails to identify some circular primes correctly.
In line 20, the printf always shows numbers that are circular primes.
Therefore, the circular_prime function sometimes goes over a true circular prime and does something to it that makes it decide that it is not a circular prime.
I suspect that the problem lies in the line
checker = remainder * pow(10, size-1) + checker/10;
under the circular_prime function. What i do is get the last digit of a number, shift all digits of a number to the right, and multiply that last digit *10 enough times to put it in front. E.g. 197 -->719
Here is what the code looks like:
/*NOTE: (int)(log(integer number)/log(10)) + 1 = number digits in a number */
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
bool prime(int);
bool circular_prime(int n, int size);
int main(void)
{
int INPUT = 200;
int i;
int counter = 0;
for(i = 2; i < INPUT; i++)
if(prime(i) && circular_prime(i, ((log(i)/log(10))+1) ) )
{
counter ++;
printf("%d \t %d\n", counter, i); //helps show which numbers are the circular ones
}
printf("The number of circular prime numbers below %d is %d", INPUT, counter);
getch();
return(0);
}
/*function checks whether a number is a prime or not */
bool prime(int n)
{.....this function works, so i cut it off from post.....}
/*functions checks for circular primes by rotating digits in the number
funtion arguments:
circular_prime(number to be checked for prime, number of digits in #) */
bool circular_prime(int n, int size)
{
int checker = n;
int remainder;
do{
remainder = checker%10;
checker = remainder * pow(10, size-1) + checker/10;
if(!prime(checker))
return false;
}while(n != checker); //stop when number is returned to original state
return true;
}
Thanks in advance!