Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C] Prime number function
#1
I'm in the process of learning C, so to exercise my loop knowledge, I created a little function to determine if a number is prime or not. A return value of 1 indicates that it is; 0 that it isn't.

Usage:
Code:
isprime(number to assess);

And here's the code:
Code:
int isprime(int x)
{
    int prime = 0;
    int i = 0;    

    for (i = (x - 1); i > 1; --i)
    {
        if ((x % i) == 0)
        {
            prime = 0;
            return prime;
        }
    
        else
        {
            prime = 1;
        }
    }
    return 1;
}

I guess beginners could learn from it. It's pretty much compatible with C++ too.
Reply
#2
(10-11-2010, 12:41 PM)cerm Wrote: I'm in the process of learning C, so to exercise my loop knowledge, I created a little function to determine if a number is prime or not. A return value of 1 indicates that it is; 0 that it isn't.

Usage:
Code:
isprime(number to assess);

And here's the code:
Code:
int isprime(int x)
{
    int prime = 0;
    int i = 0;    

    for (i = (x - 1); i > 1; --i)
    {
        if ((x % i) == 0)
        {
            prime = 0;
            return prime;
        }
    
        else
        {
            prime = 1;
        }
    }
    return 1;
}

I guess beginners could learn from it. It's pretty much compatible with C++ too.

Your code is a little redundant and could be re-factored. For example, you're really not using the prime variable. You return it in the case of true, but you don't use it in the case of false. Thus all you're really doing is needlessly setting a variable. And if you remove the prime variable entirely your ELSE statement becomes unnecessary.

And while it's completely arbitrary, the de facto value for TRUE in C is 1. Obviously if it's just you using a function it doesn't matter. But releasing code that uses unconventional values can lead to confusion. You may want to do yourself a favor and just use the standard conventions at all times. Your code will be more self-explanatory that way.

Either way, it looks like you understand the FOR loop. If you're still feeling uncomfortable with WHILE loops I'd recommend rewriting this to use one instead of FOR, just for the learning experience.

Code:
int isPrime (int x)
{
  int i = 0;    

  for (i = (x - 1); i > 1; --i)
  {
    if ((x % i) == 0)
    {
      return 1;
    }
  }
  
  return 0;
}
Ho, ho, ho! Well, if it isn't fat stinking billy goat Billy Boy in poison!
How art thou, thou globby bottle of cheap, stinking chip oil?
Come and get one in the yarbles, if ya have any yarbles, you eunuch jelly thou!
Reply
#3
I see what you're saying. Thanks for clearing me up.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  C++ stdio.h rename function problem AceInfinity 2 1,148 01-14-2012, 08:39 PM
Last Post: AceInfinity
  [C] Factorial function Commodore 1 1,031 11-28-2010, 07:13 AM
Last Post: RANA
  Problem with a member function of a class charnet3d 2 1,117 10-31-2009, 03:22 AM
Last Post: charnet3d

Forum Jump:


Users browsing this thread: 3 Guest(s)