c#实现:返回n到m之间的所有素数
class Program
{
static void Main(string[] args)
{
Sushu();
}
public static void Sushu()
{
Console.WriteLine("请输入求素数的起始数字用','隔开:");
string y = Console.ReadLine();
Console.WriteLine("这个范围内的素数有:{0}", CalPrim(int.Parse(y.Split(',')[0]), int.Parse(y.Split(',')[1]), ""));
}
public static string CalPrim(int x, int y, string sushu)
{
bool check = true;//是素数
for (int i = 2; i < x; i++)
{
if (x % i == 0)
{
check = false;
}
}
if (check == true && x < y)
{
return CalPrim(x + 1, y, sushu += x + "、");
}
else if (check == false && x < y)
{
return CalPrim(x + 1, y, sushu);
}
else
{
return sushu;
}
}
public static string GetSuShu2(int x, int y, string sushu)
{
int count = 0;
for (int i = 1; i<=x; i++)
{
if (x%i==0)
{
count++;
}
if(count>2)
return CalPrim(x+1,y,sushu);
}
if(x<y){
return CalPrim(x+1,y,sushu
+=x+"、");
}
else{
return sushu;
}
}
}