-
Notifications
You must be signed in to change notification settings - Fork 6
/
34.00 Primes in Given Range.cs
45 lines (36 loc) · 1.03 KB
/
34.00 Primes in Given Range.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.Numerics;
class PrimesInGivenRange
{
public static bool IsPrime(BigInteger number)
{
if (number == 1 || number == 0) return false;
if (number == 2) return true;
BigInteger half = number / 2;
for (int i = 2; i <= half; ++i)
{
if (number % i == 0) return false;
}
return true;
}
private static List<int> CalculatePrimesInRange(int startNumber, int endNumber)
{
List<int> numbers = new List<int>();
for (int i = startNumber; i <= endNumber; i++)
{
if (IsPrime(i))
{
numbers.Add(i);
}
}
return numbers;
}
static void Main()
{
int startNumber = int.Parse(Console.ReadLine());
int endNumber = int.Parse(Console.ReadLine());
string numbers = String.Join(", ", CalculatePrimesInRange(startNumber, endNumber).ToArray());
Console.WriteLine(numbers);
}
}