-
Notifications
You must be signed in to change notification settings - Fork 0
/
primec.csharp
147 lines (141 loc) · 4.42 KB
/
primec.csharp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
public class PrimeNumberCalculator
{
/**
* Returns a list of prime numbers up to n using the Sieve of Eratosthenes algorithm.
*/
public static List<int> SieveOfEratosthenes(int n)
{
if (n < 2)
{
return new List<int>();
}
bool[] isPrime = new bool[n + 1];
for (int i = 0; i <= n; i++)
{
isPrime[i] = true;
}
isPrime[0] = isPrime[1] = false; // 0 and 1 are not primes
for (int p = 2; p * p <= n; p++)
{
if (isPrime[p])
{
for (int multiple = p * p; multiple <= n; multiple += p)
{
isPrime[multiple] = false;
}
}
}
List<int> primes = new List<int>();
for (int p = 2; p <= n; p++)
{
if (isPrime[p])
{
primes.Add(p);
}
}
return primes;
}
/**
* Finds all prime numbers in the range [start, end].
*/
public static List<int> FindPrimesInRange(int start, int end)
{
if (start > end || start < 0 || end < 0)
{
return new List<int>();
}
List<int> primesUpToEnd = SieveOfEratosthenes(end);
List<int> primesInRange = new List<int>();
foreach (int p in primesUpToEnd)
{
if (p >= start)
{
primesInRange.Add(p);
}
}
return primesInRange;
}
/**
* Provides the last three prime numbers and the count of prime numbers in the range [start, end].
*/
public static (List<int>, int) PrimeStatistics(int start, int end)
{
List<int> primesInRange = FindPrimesInRange(start, end);
int countOfPrimes = primesInRange.Count;
List<int> lastThreePrimes = new List<int>();
if (primesInRange.Count >= 3)
{
lastThreePrimes = primesInRange.GetRange(primesInRange.Count - 3, 3);
}
else
{
lastThreePrimes = primesInRange;
}
return (lastThreePrimes, countOfPrimes);
}
/**
* Prompts the user to enter the start and end of the range, with validation.
*/
public static (int, int) GetUserInput()
{
int start, end;
while (true)
{
try
{
Console.Write("Please enter the start of the range (non-negative integer): ");
start = int.Parse(Console.ReadLine());
Console.Write("Please enter the end of the range (non-negative integer): ");
end = int.Parse(Console.ReadLine());
if (start > end)
{
Console.WriteLine("Error: Start of the range must be less than or equal to the end of the range.");
}
else if (start < 0 || end < 0)
{
Console.WriteLine("Error: Range values must be non-negative.");
}
else
{
return (start, end);
}
}
catch (FormatException)
{
Console.WriteLine("Error: Invalid input. Please enter integer values.");
}
}
}
/**
* Displays the results of the prime number calculations.
*/
public static void DisplayResults(int start, int end, List<int> lastThreePrimes, int countOfPrimes)
{
if (lastThreePrimes.Count > 0)
{
Console.Write("The last prime numbers in the range [" + start + ", " + end + "] are: ");
for (int i = 0; i < lastThreePrimes.Count; i++)
{
Console.Write(lastThreePrimes[i]);
if (i < lastThreePrimes.Count - 1)
{
Console.Write(", ");
}
}
Console.WriteLine();
}
else
{
Console.WriteLine("No prime numbers found in the range [" + start + ", " + end + "]");
}
Console.WriteLine("The number of prime numbers in this range is: " + countOfPrimes);
}
public static void Main(string[] args)
{
// Get user input for the range
var (start, end) = GetUserInput();
// Calculate and display the results
var (lastThreePrimes, countOfPrimes) = PrimeStatistics(start, end