怎么用C#里面的控制台编写输出1000以内的所有素数

来源:百度知道 编辑:UC知道 时间:2024/05/17 17:34:55

求只能被1和自己整除的正整数。

using System;
using System.Collections.Generic;
using System.Text;

namespace DataStructure
{

public class Prime
{
private static List _prime=new ArrayList();
///
/// check prime
///
///
///
public static bool isPrime(int a)
{
int i;
for (i = 2; i < a; i++)
{
if (Math.IEEERemainder((float)a, (float)i) == 0) //是否能被i整除
return false;
}
return true;
}
///
/// get all prime
///
///
///
///
public static List getPrime(int min, int max)
{
if (min < 1)
throw new Exception("min must greater than 1");
int i;
for (i = min; i <= max; i++)
{
if (isPrime(i))
_prime.add(i);
}
return _prime;
}
///
/// print all prime
///
public static void printPrime()
{
_prime.print();
}
}
}

测试:<