天天看点

找1亿以内的素数,你的电脑要跑多久?

With the C# application, it takes 4.99s.

If python, it takes about 80s. Python + numba may be close to C#.

(refer: python + numba加速 + 优化埃氏筛法:1.8秒搞定1亿内素数,14秒搞定10亿内素数 - 知乎【如果要看有关的研究过程,请移步:python搜索大范围素数:埃氏筛法的优化(初步) - zj睦.teacher的文章 - 知乎 https://zhuanlan.zhihu.com/p/401190322】优化埃氏筛法numb加速从2到10的9次方范围内素数共5084753…

找1亿以内的素数,你的电脑要跑多久?

https://zhuanlan.zhihu.com/p/400818808)

C# codes are as below:

//Sieve of Eratosthenes 埃氏筛法
        static void Main(string[] args)
        {
            DateTime t1 = DateTime.Now;
            List<Int64> lstPN = FindPrimeNumber(Convert.ToInt32(Math.Pow(10,8)));
            DateTime t2 = DateTime.Now;
            TimeSpan secondSpan = new TimeSpan(t2.Ticks - t1.Ticks);

            Console.WriteLine($"从2到10的8次方范围内素数共{lstPN.Count}个");
            Console.WriteLine($"共耗时{secondSpan.TotalSeconds}秒");
            Console.ReadLine();
        }

        static List<Int64> FindPrimeNumber(int n)
        {
            List<Int64> lstPN = new List<Int64>();
            bool[] a = Enumerable.Repeat(true, n+1).ToArray();

            for (Int64 i = 2; i < n; i++)
            {
                if (a[i])
                {
                    lstPN.Add(i);
                    for (Int64 j = i * i; j <= n; j += i)
                        a[j] = false;
                }
            }
            return lstPN;
        }