您的位置:首页 > 其它

大整数阶乘的运算(可以计算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));
}
}
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐