Advertisement
Rapptz

Prime.h

Aug 19th, 2012
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #ifndef PRIME_H
  2. #define PRIME_H
  3. #include <cmath>
  4.  
  5. bool isPrime(int n) {
  6.     if(n == 1)
  7.         return false;
  8.     else if(n < 4.0)
  9.         return true;
  10.     else if(n % 2 == 0)
  11.         return false;
  12.     else if(n < 9)
  13.         return true;
  14.     else if (n % 3 == 0)
  15.         return false;
  16.     else {
  17.         int r = floor(sqrt(static_cast<double>(n)));
  18.         int f = 5;
  19.         while (f <= r) {
  20.             if (n % f == 0) {
  21.                 return false;
  22.                  }
  23.             if (n % (f+2) == 0) {
  24.                 return false;
  25.                 }
  26.             f = f+6;
  27.         }
  28.     return true;
  29.     }
  30.  
  31. }
  32.  
  33. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement