Advertisement
martinvalchev

Primes_in_Given_Range

Feb 4th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. namespace Primes_in_Given_Range
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             int startNum = int.Parse(Console.ReadLine());
  12.             int endNum = int.Parse(Console.ReadLine());
  13.             List<int> primesInRange = FindPrimesInRange(startNum, endNum);
  14.             Console.WriteLine(string.Join(", ", primesInRange.ToArray()));
  15.         }
  16.  
  17.         static List<int> FindPrimesInRange(int startNum, int endNum)
  18.         {
  19.             List<int> primesInRange = new List<int>();
  20.             for (int i = startNum; i <= endNum; i++)
  21.             {
  22.                 if (IsPrime(i))
  23.                 {
  24.                     primesInRange.Add(i);
  25.                 }
  26.             }
  27.             return primesInRange;
  28.         }
  29.  
  30.         static bool IsPrime(long number)
  31.         {
  32.             long sqrt = (long)Math.Sqrt(number);
  33.             bool isPrime = true;
  34.             int i = 2;
  35.             if (number >= 2)
  36.             {
  37.                 while (isPrime && i <= sqrt)
  38.                 {
  39.                     if (number % i == 0) isPrime = false;
  40.                     i++;
  41.                 }
  42.             }
  43.             else isPrime = false;
  44.             return isPrime;
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement