大整数阶乘的运算(可以计算1000!) .

由于阶乘运算的增长速度特别快(比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程序。
  1. import java.util.Scanner;  
  2. /** 
  3.  * 
  4.  * @author liangbch@263.net 
  5.  */  
  6. public class Fac {  
  7.       public Fac() {  
  8.     }  
  9.       
  10.     public static void Calc(int n)  
  11.     {  
  12.         int RAD=10000;  
  13.         int buffSize=(int)(n * Math.log10((n+1)/2) / Math.log10(RAD)+1);  
  14.         short[] buff = new short[buffSize];  
  15.         int len=1;  
  16.         buff[0]=1;  
  17.         for (int i=1;i<=n;i++)  
  18.         {  
  19.             int c=0;  
  20.             for (int j=0;j<len;j++)  
  21.             {  
  22.                 int prod=(buff[j]*i+c);  
  23.                 buff[j]=(short)(prod % RAD);  
  24.                 c=prod / RAD;  
  25.             }  
  26.             while (c>0)  
  27.             {  
  28.                 buff[len++]= (short)(c % RAD);  
  29.                 c=c/RAD;  
  30.             }  
  31.         }  
  32.           
  33.         System.out.printf("%d!=%d", n, buff[len-1]);  
  34.         for (int i=len-2;i>=0;i--)  
  35.             System.out.printf("%04d",buff[i]);  
  36.      }  
  37.   
  38.     public static void main(String[] args) {  
  39.        System.out.println("Please input a integer");  
  40.        Scanner in=new Scanner(System.in);  
  41.        int n=in.nextInt();  
  42.        Calc(n);  
  43.     }  
  44. }  

Java中求阶乘的算法

 

1.一般算法:

Java代码 复制代码  收藏代码
  1. public class Factorial {   
  2.     public static int factorial(int n) {   
  3.         if (n < 0 || n > 16) {   
  4.             System.err.println("n must be great than 0 and less than 17");   
  5.             return -1;   
  6.         } else if (n == 0) {   
  7.             return 1;   
  8.         } else {   
  9.             int result = 1;   
  10.             for (int i = 1; i <= n; i++) {   
  11.                 result *= i;   
  12.             }   
  13.             return result;   
  14.         }   
  15.     }   
  16.   
  17.     public static void main(String args[]) {   
  18.         System.out.println("result = " + factorial(5));   
  19.     }   
  20. }   
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代码 复制代码  收藏代码
  1. public class Factorial {   
  2.     public static int recursion(int n) {   
  3.         if (n < 0 || n > 16) {   
  4.             System.err.println("n must be great than 0 and less than 17");   
  5.             return -1;   
  6.         } else if (n == 0) {   
  7.             return 1;   
  8.         } else {   
  9.             return n * recursion(n - 1);   
  10.         }   
  11.     }   
  12.   
  13.     public static void main(String[] args) {   
  14.         System.out.println("result = " + recursion(16));   
  15.     }   
  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代码 复制代码  收藏代码
  1. import java.math.BigInteger;   
  2.   
  3. public class Factorial {   
  4.     public static BigInteger bigInteger(int n) {   
  5.         BigInteger result = new BigInteger("1");   
  6.         if (n < 0) {   
  7.             System.err.println("n must be great than 0");   
  8.             return new BigInteger("-1");   
  9.         } else if (n == 0) {   
  10.             return new BigInteger("1");   
  11.         } else {   
  12.             for (int i = 1; i <= n; i++) {   
  13.                 BigInteger num = new BigInteger(String.valueOf(i));   
  14.                 result = result.multiply(num);   
  15.             }   
  16.             return result;   
  17.         }   
  18.     }   
  19.   
  20.     public static void main(String[] args) {   
  21.         System.out.println("result = " + bigInteger(100));   
  22.     }   
  23. }  
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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一、 课题内容和要求 1、系统的基本功能及要求 (1)写一个类BigInteger,并有如下成员函数operator+/ operator-/ operator*/ operator\,即整数的四则运算并重载运算符。 (2)用该大整数计算n的阶乘并显示出来。(n∈[1,100]) 注:为了简化,可不考虑负数,则遇上形如“2-4”这样的表达式需要报错。 2、程序执行过程 (1)系统从键盘读入自然数n,直到输入了合法的n为止。 (2)输出运算的结果,为便于观察结果,每输出四位中间插入空格,格式如下(例如,n=12): 12!= 4790 0160 0 (3)询问用户是否继续进行,用户选择“是”,直到输入合法为止,转(1),否则退出程序。 3、算法要求及提示 (1)因为n较大时,n!的结果将超出长整形的保存范围,因此结果不能用long int型的变量来保存。本算法要求用链表来存储。 (2)链表的每一个节点存储结果的一位数字,因此结果的输出实际上是链表的遍历问题,同时要先考虑用多少位来表示大整数。 4、其他要求 (1)输入时具备一定的容错性判断,如输入的不是数字,或输入的数超过范围等等。 (2)变量、函数命名符合规范。 (3)注释详细:每个变量都要求有注释说明用途;函数有注释说明功能,对参数、返回值也要以注释的形式说明用途;关键的语句段要求有注释解释。 (4)程序的层次清晰,可读性强。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值