C#求任意范围内的质数

时间:2024-05-06 09:03:14
    class Program
{
public static List<int> list;
static void Main(string[] args)
{
Console.WriteLine("请输入下限:");
int min = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入上限:");
int max = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(min+"-"+max + "以内的质数有:");
GetZhi(min, max);
foreach (int a in list)
{
Console.WriteLine(a);
}
Console.ReadLine();
} public static void GetZhi(int min,int max)
{
list = new List<int>();
if (min % == )
min = min + ;
for (int i = min; i <= max; i += )
{
bool a = true;
for (int j = ; j < i; j++)
{
if (i % j == )
{
a = false;
break;
}
}
if (a)
list.Add(i);
}
}
}