C#大数阶乘算法
来源:互联网 发布:网络剧的受众分析 编辑:程序博客网 时间:2024/05/16 11:14
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 大数阶乘{ class Program { static void Main(string[] args) { Factorial fac = new Factorial(200000); fac.Calculate(); Console.Write(fac.ToString()); Console.ReadKey(); } } public class Factorial { private int baseNumber = 100000; public int BaseNumber { get { return baseNumber; } } private int number; private List<int> result = new List<int>(); public Factorial(int n) { number = n; result = new List<int>(); } public List<int> Calculate() { int digit = (int)System.Math.Log10(baseNumber); int len = (int)(number * System.Math.Log10((number + 1) / 2) / digit); len += 2; int[] a = new int[len]; int i, j; long c; int m = 0; a[0] = 1; for (i = 2; i <= number; i++) { c = 0; for (j = 0; j <= m; j++) { long t = a[j] * i + c; c = t / baseNumber; a[j] = (int)(t % baseNumber); } while (c>0) { m++; a[m] = (int)(c % baseNumber); c = c / baseNumber; } } for (i = 0; i <= m; i++) { result.Add(a[i]); } return result; } public override string ToString() { if(result .Count ==0) { Calculate(); } StringBuilder sb = new StringBuilder(); int digit = (int)System.Math.Log10(baseNumber); sb.Append(result[result.Count - 1]); for (int i = 0; i < result.Count; i++) { sb.Append(result[i].ToString().PadLeft(digit, '0')); } return sb.ToString(); } }}
转自:<a target=_blank href="http://http://www.cnblogs.com/snowdust/archive/2010/03/18/1688963.html">点击打开链接</a>
0 0
- C#大数阶乘算法
- 大数阶乘的算法
- 大数的阶乘算法
- 大数阶乘算法
- 大数阶乘算法
- 大数的阶乘算法
- 大数阶乘算法
- 大数阶乘算法
- 大数的阶乘算法
- 求大数阶乘算法
- 大数阶乘算法
- 大数阶乘算法
- 大数阶乘算法
- 大数阶乘算法
- 大数阶乘算法
- N的阶乘(大数阶乘算法)
- 探讨大数阶乘的算法
- C++大数阶乘精确算法
- Hibernate criteria 关联
- 统计n之内的完全数
- Android开发中Handler的经典总结
- 对二叉树进行前序、中序、后序遍历
- iOS10 相关权限配置(相机、相册、日历、媒体资料库)
- C#大数阶乘算法
- git push 时出现hint错误
- 自我命名规范
- 一个Thinkphp、jquery ajax、IE10以下浏览器的BUG冲突问
- spring,mybatis事务管理 隔离级别与事务传播
- Strus2_OGNL表达式
- Spring Boot 配置文件 - 属性大全
- Android 5.x 权限问题解决方法
- Unsupported class version number [52.0] (maximum 51.0, Java 1.7))