栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

找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…https://zhuanlan.zhihu.com/p/400818808)

C# codes are as below:

        //Sieve of Eratosthenes 埃氏筛法
        static void Main(string[] args)
        {
            DateTime t1 = DateTime.Now;
            List 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 FindPrimeNumber(int n)
        {
            List lstPN = new List();
            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;
        }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/929195.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号