wingman007

PrimeNumbers

Sep 16th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1.     // A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. The smallest prime number is 2, since its only divisors are 1 and 2.
  2.     // The prime number at position 1 is: 2
  3.     // http://www.geocachingtoolbox.com/index.php?page=primeNumbers
  4.  
  5.             Console.WriteLine("Prime numbers:");
  6.             bool isPrime = true;
  7.             for (i = 2; i < 100; i++)
  8.             {
  9.                 isPrime = true;
  10.                 for (int j = 2; j < i; j++)
  11.                 {
  12.                     // if (i % j == 0)
  13.                     if (i % Math.Sqrt(j) == 0) // better
  14.                     {
  15.                         isPrime = false;
  16.                         break;
  17.                     }  
  18.                 }
  19.                 if (isPrime) {
  20.                     Console.WriteLine(i);
  21.                 }
  22.             }
Add Comment
Please, Sign In to add comment