• 大整数阶乘的运算(可以计算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. }   

       运行结果: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. }  

     运行结果: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. }  

     运行结果:result = 93326215443944152681699238856266700490715968264381621468592963895

    217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

  • 相关阅读:
    宋亡之后无中国,明亡之后无华夏——有多少人懂
    关于Verilog 中的for语句的探讨
    三种不同状态机写法
    异步复位和同步复位
    转载
    FIFO认识(一)
    Quartus II管脚批量分配文件(.tcl)格式
    mif文件C语言生成
    基于FPGA的HDMI显示设计(三)
    FPGA----只读存储器(ROM)
  • 原文地址:https://www.cnblogs.com/alamps/p/2726016.html
Copyright © 2020-2023  润新知