为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

9999阶乘

2017-11-15 4页 doc 15KB 26阅读

用户头像

is_601191

暂无简介

举报
9999阶乘9999阶乘 using System; using System.Collections.Generic; using System.Linq; using Oyster.Math; namespace csdnTest { class Program { static void Main(string[] args) { DateTime beginTime = DateTime.Now; IntX result = Factorial(1000000); DateTime endTime = D...
9999阶乘
9999阶乘 using System; using System.Collections.Generic; using System.Linq; using Oyster.Math; namespace csdnTest { class Program { static void Main(string[] args) { DateTime beginTime = DateTime.Now; IntX result = Factorial(1000000); DateTime endTime = DateTime.Now; Console.WriteLine(endTime - beginTime); Console.WriteLine(result); Console.ReadKey(); } static IntX Factorial(int n) { int[] counter = GetPowCounter(n); SortedDictionary sDict = new SortedDict ionary(); //计算幂乘并将结果压入优先队列(使用优化过的大数乘 法,在计算相等规模的大数乘法时,效率最高) for (int i = 2; i <= n; i++) { if (counter[i] > 0) sDict.Add(IntX.Pow(i, (uint)counter[i]), fa lse); } IntX valueA = 1, valueB; //用SortedDictionary模拟优先队列进行最后的计算 while (sDict.Count > 1) { valueA = sDict.ElementAt(0).Key; valueB = sDict.ElementAt(1).Key; sDict.Remove(valueA); sDict.Remove(valueB); sDict.Add(valueA * valueB, false); } return sDict.ElementAt(0).Key; } //做质因数分解,以便使用幂乘进行计算 static int[] GetPowCounter(int n) { int[] pList = GetPrime(n); int[] pCounter = new int[n + 1]; for (int i = 0; i < pList.Length; i++) { int k = n; while ((k /= pList[i]) > 0) pCounter[pList[i]] += k; } return pCounter; } //生成质数列表 static int[] GetPrime(int n) { List prime = new List(); bool[] flags = new bool[n + 1]; for (int i = 2; i <= n; i++) { if (!flags[i]) prime.Add(i); for (int j = 0; j < prime.Count; j++) { if (prime[j] * i > n) break; flags[prime[j] * i] = true; if (i % prime[j] == 0) break; } } return prime.ToArray(); } } }
/
本文档为【9999阶乘】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索