2022年5月21日 星期六

列出某數字以下的所有質數

 這應該是我第三次做這個練習了,每次都用不同的語言,這次是用 C# ,寫起來比 python 方便,據說 Fortran 做數值運算比其他語言有效率,或許可以試看看它有多快。

using System;

namespace PrimeNumber

{

    class Program

    {

        static void Main(string[] args)

        {

            bool u;

            int sum = 0;

            for (int hei = 5; hei < 1000000000; hei += 2)

            {

                u = true;

                for (int hej = 3; hej <= (hei / 2); hej += 2)

                {

                    if (hei % hej == 0)

                    {

                        u = false;

                        break;

                    }

                }

                if (u)

                {

                    Console.Write(hei + " ");

                    sum++;

                }

            }

        }

    }

}





沒有留言:

張貼留言