Advertisement
Guest User

List of Primes

a guest
Sep 30th, 2016
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _07.Primes_in_Given_Range
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             long start = long.Parse(Console.ReadLine());
  11.             long end = long.Parse(Console.ReadLine());
  12.             List<long> primeList = FindPrimesInRange(start, end);
  13.             string numbers = String.Join(", ", primeList.ToArray());
  14.             Console.WriteLine(numbers);
  15.         }
  16.  
  17.         static List<long> FindPrimesInRange(long startNum, long endNum)
  18.         {
  19.             List<long> list = new List<long>();
  20.             for (long i = startNum; i < endNum; i++)
  21.             {
  22.                 if (IsPrime(i))
  23.                 {
  24.                     list.Add(i);
  25.                 }
  26.             }
  27.             return list;
  28.         }
  29.  
  30.         private static bool IsPrime(long n)
  31.         {
  32.             bool prime = true;
  33.             if (n <= 1)
  34.             {
  35.                 prime = false;
  36.             }        
  37.             else
  38.             {
  39.                 for (int i = 2; i <= Math.Sqrt(n); i++)
  40.                 {
  41.                     if (n % i == 0)
  42.                     {
  43.                         prime = false;
  44.                         break;
  45.                     }
  46.                 }
  47.             }
  48.             return prime;
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement