Advertisement
Guest User

IsPrime (C#)

a guest
Feb 13th, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.33 KB | None | 0 0
  1. public static bool IsPrime(ulong N)
  2. {
  3.     if ((N & 1) == 0) return N == 2;
  4.     if (N % 3 == 0) return N == 3;
  5.     if (N == 1) return false;
  6.     if (N > 0xfffffff9ffffffc5) throw new OverflowException();
  7.  
  8.     ulong p = 1, x = 2, max = (ulong)Math.Sqrt(N);
  9.     while ((p += (x ^= 6)) <= max)
  10.         if (N % p == 0)
  11.             return false;
  12.     return true;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement