Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

29
Mar

inline bool isprime()

Related Blog Items

This is one of the more simple snippets I could actually write, and one of the best memorized pieces of code that I can actually write as the starting post of this blog. For interested bloggers who would want to be able to post their snippets here, contact me (Dean Michael Berris) through email (see page description for updated email information).

inline bool isprime(unsigned long int number)
{
  if (number <= 3)
    return true;

  if ((number % 2) == 0)
    return false;

  for (unsigned register int i = 5; i * i <= number; i+=2)
  {
    if ((number % i) == 0)
      return false;
  }
  return true;
}

Comments and optimizations most welcome.

creative commonsThis work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.

Popularity: 6%

You need to log on to convert this article into PDF


Related Blog Items

2 Comments

  • Rajaganesh  said:

    in the loop,
    you don’t need to check until the number itself..checking until the square root of that number is enough…

    for (unsigned register int i = 5; i * i


  • Rajaganesh  said:

    for (unsigned register int i = 5; i * i


Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image