C#找質數(素數)

C#找質數(素數)

質數(prime number)又稱素數,有無限個。指整數在一個大於1的自然數中,除了1和此整數自身外,沒法被其他自然數整除的數。換句話說,只有兩個正因數(1和自己)的自然數即爲素數(除了1和它本身以外不再有其他的因數)。根據算術基本定理,每一個比1大的整數,要麼本身是一個質數,要麼可以寫成一系列質數的乘積,比1大但不是素數的數稱爲合數。1和0既非素數也非合數。最小的質數是2。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ConAppPrimeNumber
{

    class Program
    {
        static void Main(string[] args)
        {

            System.Diagnostics.Stopwatch s = new Stopwatch();

            //s.Start();
            //PrimeNumber.Find3();
            //s.Stop();
            //System.Console.WriteLine("Watch Time:{0}m, {1}s, {2}ms", s.Elapsed.Minutes, s.Elapsed.Seconds, s.ElapsedMilliseconds);

            //System.Console.WriteLine();

            s.Reset();
            s.Start();
            PrimeNumber.Find4();
            s.Stop();
            System.Console.WriteLine("Watch Time:{0}m, {1}s, {2}ms", s.Elapsed.Minutes, s.Elapsed.Seconds, s.ElapsedMilliseconds);

        }
    }


    public class PrimeNumber
    {
        static int n = 100000;      //查找10w(n=10w)以內的素數    
        //static int n = 1000000;   //100w    
        static bool IsPN = false;   //找到素數就設置爲true
        static int count = 0;       //計數器

        //找奇數
        public static void Find()
        {
            for (int i = 1; i < n; i++)
            {
                if (i % 2 == 0) continue;//先過濾能被2整除的數,%表示取模,類似delphi中的Mod
                System.Console.Write(i + "\t");
            }
        }

        //找奇數,優化代碼
        public static void Find2()
        {
            for (int i = 1; i < n; i = i + 2)//通過循環步進值得到奇數
            {
                System.Console.Write(i + "\t");
            }
        }

        //找質數(只能被1和本身整除的數就是質數),質數同時也叫素數//查詢找10W以內的,需要1秒
        public static void Find3()
        {
            IsPN = false;
            System.Console.Write(2 + "\t" + 3 + "\t");//因爲下面優化代碼,所以2和3必須在這裏輸出
            count = 2;

            for (int i = 1; i < n; i += 2)//先找出1,3,5,7,9。。。奇數
            {
                for (int j = 3; j < i; j += 2)//從3開始,比如3,5,7。。。,把1,2,4,6。。。去掉
                {
                    if (i % j == 0)
                    {
                        IsPN = false;//不是質數
                        break;
                    }
                    IsPN = true;
                }
                if (IsPN)
                {
                    System.Console.Write(i + "\t");   //需要看Watch,請註釋此語句
                    count++;
                }
            }
            System.Console.WriteLine("Count: {0}", count);
        }

        //找質數,優化代碼//查找1000W以內6秒
        public static void Find4()
        {
            IsPN = false;
            System.Console.Write(2 + "\t" + 3 + "\t");
            count = 2;

            for (int i = 1; i < n; i += 2)
            {
                for (int j = 3; j < i; j += 2)
                {
                    if (i % j == 0)
                    {
                        IsPN = false;
                        break;
                    }

                    //被除數的平方大於這個數則不必判斷,拿97來說,當除以3/5/7/9/11之後還有餘數,後面的13/15/17,其實是沒必要再驗證了,因爲11*11已經大於97
                    //這裏本是要對i開平方根,i % j==0,j的取值範圍是小於i的平方根以內的所有素數,因爲不好實現,所以我改用質數的平方
                    if (j * j > i)
                    {
                        IsPN = true;
                        break;
                    }
                }

                if (IsPN)
                {
                    System.Console.Write(i + "\t");   //需要看Watch,請註釋此語句
                    count++;
                }
            }
            System.Console.WriteLine("Count: {0}", count);
        }        

    }
}

版權所有,轉載請註明文章出處 http://blog/csdn.net/cadenzasolo

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章