素数筛法算法(C#)

来源:互联网 发布:linux查看log日志命令 编辑:程序博客网 时间:2024/06/06 05:07
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();        }    }}


0 0
原创粉丝点击