Prime Number checker

By Sonny on Apr 22, 2010

A simple function to check if a number is a prime or not. It will return true or false.

Edit: Please don't leave a blunt comment saying prime numbers aren't useful... Prime numbers are ONLY useful in computing and CS... They are a key feature in security. And unless you can list every prime number off the top of your head, this is a useful resource.

bool pnumf(const int num)
{
  int f=0;
  bool p;
  for (int a=num;a>1;a--)
  {
    f = (num%a == 0) ? f+1 : f;
  }
  p = (f>1 || num==1) ? false : true;
  return p;
}

Comments

Sign in to comment.
rsb   -  Apr 26, 2010

Kinda nifty

 Respond  
jsg55   -  Apr 22, 2010

heh i didnt mean it as an insult to you, just meant that it was wrong to say what he said. and i know what you mean lol

 Respond  
Sonny   -  Apr 22, 2010

@jsg55 While I'm not a beginner to the language, I am still "learning". But, stronger codes that I do implement, like encryption key generators (in which I do use this function), I don't post. As I do not want it to be public code. I will post small/medium sized codes that will aide people in developing diverse and somewhat simliar codes. A lot of people just don't understand the use of prime numbers, or even more simplistic mathematical factions.

 Respond  
Sonny   -  Apr 22, 2010

@bielle It's useful if you're using it inline. It's not meant to return the prime number.
@ssimar If you're going to reply with a blatant, and disrespectful comment, don't bother leaving one.

A very useful resource in finding prime numbers is stronger encryption. As there are only so many factors when multiiplying them. When multiplying two prime numbers together instead of any two ordinary numbers, there are only four factors, making the encryption key stronger, which makes it harder to crack.

 Respond  
ssimar   -  Apr 22, 2010

Boring

 Respond  
jsg55   -  Apr 22, 2010

hey it might just be me but i dont think your supposed to just insult the persons code, you should tell them why you think its weak and give them constructive critisism (didnt spell that right), that kind of stuff is going to discourage a beggining scripter really badly. especially one trying to learn a language like this one lol.

 Respond  
Bielie   -  Apr 22, 2010

srry but i dont think its usefull.

true = 1
false = 0

srry but thats just weak........

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.