表面粗糙度国家标准:怎么用C#里面的控制台编写输出1000以内的所有素数

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/01 03:34:06

求只能被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();
}
}
}

测试:

Prime.getPrime(2, 60);
Prime.printPrime();

结果:

2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59

你算1000就换一下了,明白了吗

只能被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();
}
}
}

测试:

Prime.getPrime(2, 60);
Prime.printPrime();

结果:

2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59

你算1000就换一下了,明白了吗。