Advertisement
Filkolev

Find Primes

Jan 16th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System;
  2.  
  3. class FindPrimes
  4. {
  5.     static void Main()
  6.     {
  7.         int countPrimes = 1;
  8.         int number = 3;
  9.  
  10.         while (countPrimes < 251)
  11.         {
  12.             bool isPrime = true;
  13.  
  14.             for (int divisor = 2; divisor <= Math.Sqrt(number); divisor++)
  15.             {
  16.                 if (number % divisor == 0)
  17.                 {
  18.                     isPrime = false;
  19.                     break;
  20.                 }
  21.             }
  22.  
  23.             if (isPrime)
  24.             {
  25.                 countPrimes++;
  26.  
  27.                 if (countPrimes == 24 || countPrimes == 101 || countPrimes == 251)
  28.                 {
  29.                     Console.WriteLine(
  30.                         "The {0}{1} prime number is: {2}",
  31.                         countPrimes, countPrimes == 24 ? "th" : "st", number);
  32.                 }
  33.             }
  34.  
  35.             number++;
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement