Thread: Here is my prime code! why wont it work?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    Question Here is my prime code! why wont it work?

    The prime number is passed into this function. This works fine for 7 but when I enter 407 it tells me it is prime when it is not.

    Here goes

    char test_prime(int prime)

    {

    int r; /*remainder*/
    int i;

    for (i =2; i < prime;i++)
    if (prime % i == 0)
    {
    return('n'); /*not prime*/
    }
    else
    {
    return('p');
    }

    Thanks guys
    Joe

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    49
    Your code does not work for prime=9, it returns 'p'.
    Your "if ... else" is not good.

    For i=2
    9 % i = 1 != 0
    so your prog goes to "else" and returns 'p' !!!
    Just take off the "else" keyword ...

    In fact your prog only looks for odd numbers returning 'p'.

    some hints : your loop can be shorter
    for (i =2; i < prime/2;i++)

    for (i =2; i < sqrt(prime)+1;i++)

    excepted i=2, you can consider only odd i.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    you have to use r, to check with..
    example...


    int r; /* r is the rest */

    int test_prime(int prime)
    {
    int i;
    for (i=2; i < prime; i++)
    while(r == prime%i)
    {
    if(r == 0)
    {
    printf("Not primtal: %d", prime);
    return 0;
    }
    else
    {
    printf("This is a primtal: %d", prime);
    return 0;
    }
    }
    return 0;
    }

    int main()
    {
    test_prime(407);
    getchar();
    return 0;
    }
    !G!

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    cprogramnewbie
    Your loop is no loop. As GertFaller stated, when the program goes into the for-loop, it immediately returns.

    Much easier would be to extend your condition. The example below shows how such is done, in case you didn't know such is possible.

    Further you should use return only at the end of your function. In general this makes a program much easier to read and to understand. I always introduce a return value which value is set by the code in the function and returned at the end.

    Code:
    is_prime = TRUE
    for (i = 2; i < prime && is_prime == TRUE; i++)
    {
        if (prime % i == 0)
            is_prime = FALSE;
    }
    
    return (is_prime == FALSE) : 'n' ? 'p';
    Gugge

    Also to you, don't use so much returns in your function. You also have logic error in your code. In your while loop, which is not really a while loop, an if-else construction is used which results in an immediate return. So the last return in the function will never be reached. To avoid problems like this, it is better to keep a return value.

    Further, assume

    prime = 9
    i = 2

    then

    9 % 2 == 1

    So your function says: that it is a prime?

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    tx for correcting me. I just saw that.....
    !G!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  2. Replies: 9
    Last Post: 09-09-2007, 08:08 AM
  3. Simple C++ code doesn't work
    By alex_dude_122 in forum C++ Programming
    Replies: 6
    Last Post: 10-18-2006, 12:53 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM