演算法 之 哥德巴哈猜想

來源:互聯網
上載者:User
using System;namespace Test{    class Program    {        /// <summary>        /// 判斷素數        /// </summary>        /// <param name="n"></param>        /// <returns></returns>        static bool IsPrime(int n)        {            int j = (int)Math.Ceiling(Math.Sqrt(Convert.ToDouble(n)));            bool intFlag = true;            for (int i = 1; i <= j; i++)            {                if (n % i == 0 && (i != 1) && (i != n))                {                    intFlag = false;                    break;                }            }            return intFlag;        }        /// <summary>        /// 輸入一個數判斷是否符合哥德巴哈猜想        /// 大於6的偶數都可以表示成為兩個素數之和        /// </summary>        /// <param name="n"></param>        /// <returns></returns>        static bool IsGDBHArish(int n)        {            bool isG = false;            if (n % 2 == 0 && n > 6)            {                for (int i = 1; i < n / 2; i++)                {                    bool b1 = IsPrime(i);                    bool b2 = IsPrime(n - i);                    if (b1 & b2)                    {                        Console.WriteLine("{0}={1}+{2}", n, i, n - i);                        isG = true;                    }                }            }            return isG;        }               static void Main(string[] args)        {            //////素數            int n = 1;            while (n != -1)            {                Console.WriteLine("請輸入一個數:");                if (Int32.TryParse(Console.ReadLine(), out n))                {                    if (n == -1)                        break;                    //if (Program.IsPrime(n))                    //{                    //    Console.WriteLine("{0}是素數", n);                    //}                    //else                    //{                    //    Console.WriteLine("{0}不是素數", n);                    //}                    IsGDBHArish(n);                }                            }            Console.ReadLine();        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.