大整数阶乘的运算(可以计算1000!) .
2012-10-16 14:42
513 查看
由于阶乘运算的增长速度特别快(比2^n的增长速度快),对于较小整数的阶乘运算采用简单的递规算法可以实现,但是对于大整数的乘法(比如1000!),则传统的递规算法就失去了作用。
由于本人的水平不高,用下列拙劣的方式实现,请高人多多指教。具体如下:定义一个很长的数组,用数组的每一项表示计算结果的每一位。例如,7!=5040,a[1000],则a[0]=0,a[1]=4,a[2]=0,a[3]=5。
程序源代码:
/**
*计算大数的阶乘,算法的主要思想就是将计算结果的每一位用数组的一位来表示:如要计算5!,那么首先将
*(1) a[0]=1,然后a[0]=a[0]*2,a[0]=2,
*(2) a[0]=a[0]*3,a[0]=6
*(3) a[0]=a[0]*4,a[0]=24,此时a[1]=2,a[0]=4
*/
public class Factorial
{
static int a[] = new int [10000];
static void factorial(int n)
{
for(int i=2; i< a.length; i++)
a[i] = 0; //将数组元素初始化
a[0] = 1; //用数组的一项存放计算结果的位数
a[1] = 1; //将第一项赋值为一
for(int j= 2; j <= n; j++)
{
int i=1;
int c = 0; //c表示向高位的进位
for(; i <= a[0]; i++)
{
a[i] = a[i] * j + c;//将来自低位的计算结果和本位的结果相加
c = a[i] / 10;
a[i] = a[i] % 10;
}
for(; c != 0; i++)
{
a[i] = c%10;
c = c / 10;
}
a[0] = i - 1;
}
}
public static void main(String[] args)
{
String num = args[0];
int count = 0;
int n = Integer.parseInt(num);
f(n);
for(int i= a[0]; i>0; i--)
{
count++;
System.out.print(/*"a[" + i + "]=" + */a[i]/* + " "*/);
}
System.out.println("/n"+count);
}
}
======================================
本文给出Java语言版的计算大数阶乘的程序,本文使用动态数组的存储计算过程的中间结果和最终结果。每个short型数组元素表示4位10进制数。顺便说一下,这是我的第一个Java程序。
[java] view plaincopyprint?
import java.util.Scanner;
/**
*
* @author liangbch@263.net
*/
public class Fac {
public Fac() {
}
public static void Calc(int n)
{
int RAD=10000;
int buffSize=(int)(n * Math.log10((n+1)/2) / Math.log10(RAD)+1);
short[] buff = new short[buffSize];
int len=1;
buff[0]=1;
for (int i=1;i<=n;i++)
{
int c=0;
for (int j=0;j<len;j++)
{
int prod=(buff[j]*i+c);
buff[j]=(short)(prod % RAD);
c=prod / RAD;
}
while (c>0)
{
buff[len++]= (short)(c % RAD);
c=c/RAD;
}
}
System.out.printf("%d!=%d", n, buff[len-1]);
for (int i=len-2;i>=0;i--)
System.out.printf("%04d",buff[i]);
}
public static void main(String[] args) {
System.out.println("Please input a integer");
Scanner in=new Scanner(System.in);
int n=in.nextInt();
Calc(n);
}
}
Java中求阶乘的算法
1.一般算法:
Java代码
public class Factorial {
public static int factorial(int n) {
if (n < 0 || n > 16) {
System.err.println("n must be great than 0 and less than 17");
return -1;
} else if (n == 0) {
return 1;
} else {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}
public static void main(String args[]) {
System.out.println("result = " + factorial(5));
}
}
运行结果:result = 120
2.递归算法:
Java代码
public class Factorial { public static int recursion(int n) { if (n < 0 || n > 16) { System.err.println("n must be great than 0 and less than 17"); return -1; } else if (n == 0) { return 1; } else { return n * recursion(n - 1); } } public static void main(String[] args) { System.out.println("result = " + recursion(16)); } }
运行结果:result = 2004189184
3.使用BigInteger
Java代码
import java.math.BigInteger; public class Factorial { public static BigInteger bigInteger(int n) { BigInteger result = new BigInteger("1"); if (n < 0) { System.err.println("n must be great than 0"); return new BigInteger("-1"); } else if (n == 0) { return new BigInteger("1"); } else { for (int i = 1; i <= n; i++) { BigInteger num = new BigInteger(String.valueOf(i)); result = result.multiply(num); } return result; } } public static void main(String[] args) { System.out.println("result = " + bigInteger(100)); } }
运行结果:result = 93326215443944152681699238856266700490715968264381621468592963895
217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
由于本人的水平不高,用下列拙劣的方式实现,请高人多多指教。具体如下:定义一个很长的数组,用数组的每一项表示计算结果的每一位。例如,7!=5040,a[1000],则a[0]=0,a[1]=4,a[2]=0,a[3]=5。
程序源代码:
/**
*计算大数的阶乘,算法的主要思想就是将计算结果的每一位用数组的一位来表示:如要计算5!,那么首先将
*(1) a[0]=1,然后a[0]=a[0]*2,a[0]=2,
*(2) a[0]=a[0]*3,a[0]=6
*(3) a[0]=a[0]*4,a[0]=24,此时a[1]=2,a[0]=4
*/
public class Factorial
{
static int a[] = new int [10000];
static void factorial(int n)
{
for(int i=2; i< a.length; i++)
a[i] = 0; //将数组元素初始化
a[0] = 1; //用数组的一项存放计算结果的位数
a[1] = 1; //将第一项赋值为一
for(int j= 2; j <= n; j++)
{
int i=1;
int c = 0; //c表示向高位的进位
for(; i <= a[0]; i++)
{
a[i] = a[i] * j + c;//将来自低位的计算结果和本位的结果相加
c = a[i] / 10;
a[i] = a[i] % 10;
}
for(; c != 0; i++)
{
a[i] = c%10;
c = c / 10;
}
a[0] = i - 1;
}
}
public static void main(String[] args)
{
String num = args[0];
int count = 0;
int n = Integer.parseInt(num);
f(n);
for(int i= a[0]; i>0; i--)
{
count++;
System.out.print(/*"a[" + i + "]=" + */a[i]/* + " "*/);
}
System.out.println("/n"+count);
}
}
======================================
本文给出Java语言版的计算大数阶乘的程序,本文使用动态数组的存储计算过程的中间结果和最终结果。每个short型数组元素表示4位10进制数。顺便说一下,这是我的第一个Java程序。
[java] view plaincopyprint?
import java.util.Scanner;
/**
*
* @author liangbch@263.net
*/
public class Fac {
public Fac() {
}
public static void Calc(int n)
{
int RAD=10000;
int buffSize=(int)(n * Math.log10((n+1)/2) / Math.log10(RAD)+1);
short[] buff = new short[buffSize];
int len=1;
buff[0]=1;
for (int i=1;i<=n;i++)
{
int c=0;
for (int j=0;j<len;j++)
{
int prod=(buff[j]*i+c);
buff[j]=(short)(prod % RAD);
c=prod / RAD;
}
while (c>0)
{
buff[len++]= (short)(c % RAD);
c=c/RAD;
}
}
System.out.printf("%d!=%d", n, buff[len-1]);
for (int i=len-2;i>=0;i--)
System.out.printf("%04d",buff[i]);
}
public static void main(String[] args) {
System.out.println("Please input a integer");
Scanner in=new Scanner(System.in);
int n=in.nextInt();
Calc(n);
}
}
Java中求阶乘的算法
1.一般算法:
Java代码
public class Factorial {
public static int factorial(int n) {
if (n < 0 || n > 16) {
System.err.println("n must be great than 0 and less than 17");
return -1;
} else if (n == 0) {
return 1;
} else {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}
public static void main(String args[]) {
System.out.println("result = " + factorial(5));
}
}
public class Factorial { public static int factorial(int n) { if (n < 0 || n > 16) { System.err.println("n must be great than 0 and less than 17"); return -1; } else if (n == 0) { return 1; } else { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } } public static void main(String args[]) { System.out.println("result = " + factorial(5)); } }
运行结果:result = 120
2.递归算法:
Java代码
public class Factorial { public static int recursion(int n) { if (n < 0 || n > 16) { System.err.println("n must be great than 0 and less than 17"); return -1; } else if (n == 0) { return 1; } else { return n * recursion(n - 1); } } public static void main(String[] args) { System.out.println("result = " + recursion(16)); } }
public class Factorial { public static int recursion(int n) { if (n < 0 || n > 16) { System.err.println("n must be great than 0 and less than 17"); return -1; } else if (n == 0) { return 1; } else { return n * recursion(n - 1); } } public static void main(String[] args) { System.out.println("result = " + recursion(16)); } }
运行结果:result = 2004189184
3.使用BigInteger
Java代码
import java.math.BigInteger; public class Factorial { public static BigInteger bigInteger(int n) { BigInteger result = new BigInteger("1"); if (n < 0) { System.err.println("n must be great than 0"); return new BigInteger("-1"); } else if (n == 0) { return new BigInteger("1"); } else { for (int i = 1; i <= n; i++) { BigInteger num = new BigInteger(String.valueOf(i)); result = result.multiply(num); } return result; } } public static void main(String[] args) { System.out.println("result = " + bigInteger(100)); } }
import java.math.BigInteger; public class Factorial { public static BigInteger bigInteger(int n) { BigInteger result = new BigInteger("1"); if (n < 0) { System.err.println("n must be great than 0"); return new BigInteger("-1"); } else if (n == 0) { return new BigInteger("1"); } else { for (int i = 1; i <= n; i++) { BigInteger num = new BigInteger(String.valueOf(i)); result = result.multiply(num); } return result; } } public static void main(String[] args) { System.out.println("result = " + bigInteger(100)); } }
运行结果:result = 93326215443944152681699238856266700490715968264381621468592963895
217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
相关文章推荐
- 大整数阶乘的运算(可以计算1000!)
- 计算大整数(比如1000)的阶乘
- ACM 大数运算 【计算1000以内的阶乘】
- 蓝桥杯 - 基础练习 阶乘计算 -整数数组 耗时低~可以通过
- 用递归方法计算整数n的阶乘n!
- XML - position()函数的返回值是整数,可以使用mod运算符来计算余数
- 习题10:参照Windows系统“附件”中的“计算器”,自行编写一个简易的计算器。要求:可以实现由0~4构成的整数的加减运算。
- 为什么异或运算可以实现两个整数的交换,而无需借助第3个临时变量
- 1042: 计算一个整数N的阶乘
- 超精度运算之大整数求阶乘
- 九度OJ 1076 N的阶乘 (大整数运算)
- 计算一个整数的阶乘
- java中正则表达式用Pattern计算字符串的结果(四则运算);分成有括号和没括号;当然也可以采用逆波兰式
- 假如一个数组存储了一个股票,在一天交易窗口内各时间点的股票价格(正整数),只允许一次买入和一次卖出,请提供一个算法,计算出通过买入和卖出可以得到的最大利润
- 阶乘计算升级版(大整数)
- JS 浮点运算精度解决办法 (把小数转换为整数进行计算【但是不能超过js的整数上限】)
- JAVA--第十周作业编写之一个Teacher类负责给出算术题目,随机给出两个整数并进行运算,并判断回答者的答案是否正确;编写一个GUI类ComputerFrame,回答者可以通过GUI看到题目并给出
- java计算器,可以运算括号
- 基础练习 阶乘计算 (大整数Bignteger的应用)
- 解题笔记(38)——大整数阶乘计算