Advertisement
Guest User

PrimeTool

a guest
Feb 22nd, 2015
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace FindPrimes
  8. {
  9. public static class PrimeTool
  10. {
  11. public static bool IsPrime(ulong candidate)
  12. {
  13. // Test whether the parameter is a prime number. Source: http://www.dotnetperls.com/prime
  14. if ((candidate & 1) == 0)
  15. {
  16. if (candidate == 2)
  17. {
  18. return true;
  19. }
  20. else
  21. {
  22. return false;
  23. }
  24. }
  25. // Note:
  26. // ... This version was changed to test the square.
  27. // ... Original version tested against the square root.
  28. // ... Also we exclude 1 at the end.
  29. for (ulong i = 3; (i * i) <= candidate; i += 2)
  30. {
  31. if ((candidate % i) == 0)
  32. {
  33. return false;
  34. }
  35. }
  36. return candidate != 1;
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement