Member Avatar for ericstern

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!

A large part of the problem (I cannot speak for your algorithm as of yet because I haven't been able to compile it without forcing) is that the math.h functions have require signatures that for the most part are float and double (and definitely not int), for example pow has the following prototypes (in C, there are a few others in C++)

#include <math.h>
	double 	pow (double x, double y)
	long 	powl (long double x, long double y)
	float 	powf (float x, float y)
(see  http://www.codecogs.com/d-ox/c/math.h/index.php to check what types of arguments they do take)

So what you need to do is make some design decisions as to whether you should cast these variables going into log, pow, etc. and/or explicitly put 10.0 instead of 10 etc.

Hey buddy ,I think your Algorithm is awsome but there may be some error in your code.You forgot to not to count the number which is come again in for loop.e.g. If we want to find out how many circular prime below 50 then your code would give 9 because it will count 31 as a circular prime.But Actually it won't count as circular prime.Actual answer have to 8 because it would not count 31 as a circular prime but your code did.Should i right?

Please i have doubt in above section.Some one please try to solve it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.