Advertisement
Rapptz

Untitled

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