您的位置:首页 > 编程语言 > C#

素数筛法算法(C#)

2016-11-06 16:44 274 查看
using System;
public class PrimeFilter
{
    public static void Main(string[] args)
    {
        int N = 100;
        bool[] a = new bool[N + 1];
        for (int i = 2; i <= N; i++)
        {
            a[i] = true;
        }
        for (int i = 2; i < N; i++)
        {
            if (a[i])
            {
                for (int j = i * 2; j <= N; j += i)
                {
                    a[j] = false;
                }
            }
        }
        for (int i = 2; i <= N; i++)
        {
            if (a[i])
            {
                Console.Write(i + " ");
            }
        }
        Console.ReadKey();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrimeFilter
{
class Program
{
static void Main(string[] args)
{
int N = 100;
bool[] a = new bool[N + 1];
for (int i = 2; i <= N; i++)
{
a[i] = true;
}
for (int i = 2; i < N; i++)
{
if (a[i])
{
for (int j = i * 2; j <= N; j += i)
{
a[j] = false;
}
}
}
for (int i = 2; i <= N; i++)
{
if (a[i])
{
Console.Write(i + " ");
}
}
Console.ReadKey();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: