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);


/*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;
        remainder = checker%10;
        checker =  remainder * pow(10, size-1) + checker/10;
            return false;
    }while(n != checker);     //stop when number is returned to original state
    return true;

Thanks in advance!

7 Years
Discussion Span
Last Post by jonsca

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.

Edited by jonsca: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.