Advertisement
EmoRz

SieveOfErathosthenes

May 27th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace SieveOfEratosthenes
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             var n = int.Parse(Console.ReadLine());
  11.             List<int> sequnceOfPrimeNumbers = new List<int>();
  12.             var isPrime = false;
  13.             for (int i = 2; i <= n; i++)
  14.             {
  15.                 isPrime = CheckForPrimeNumberInSequence(i);
  16.                 if (isPrime)
  17.                 {
  18.                     sequnceOfPrimeNumbers.Add(i);
  19.                 }
  20.             }
  21.             Console.WriteLine(string.Join(" ", sequnceOfPrimeNumbers));
  22.         }
  23.         private static bool CheckForPrimeNumberInSequence(int n)
  24.         {
  25.             bool isPrime = true;
  26.             for (int i = 2; i <= Math.Sqrt(n); i++)
  27.             {
  28.                 if (n % i == 0)
  29.                 {
  30.                     isPrime = false;
  31.                     break;
  32.                 }
  33.             }
  34.             return isPrime;
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement