Calculation of four factorial in Java

Source: Internet
Author: User

packagecom.zf.s2;//创建一个包importjava.math.BigInteger;//导入类importjava.util.ArrayList;importjava.util.List;publicclassTextFactorial {//操作计算阶乘的类    publicstaticintsimpleCircle(intnum){//简单的循环计算的阶乘        intsum=1;        if(num<0){//判断传入数是否为负数            thrownewIllegalArgumentException("必须为正整数!");//抛出不合理参数异常        }        for(inti=1;i<=num;i++){//循环num            sum *= i;//每循环一次进行乘法运算        }        returnsum;//返回阶乘的值    }    publicstaticintrecursion(intnum){//利用递归计算阶乘        intsum=1;        if(num < 0)            thrownewIllegalArgumentException("必须为正整数!");//抛出不合理参数异常        if(num==1){            return1;//根据条件,跳出循环        }else{            sum=num * recursion(num-1);//运用递归计算            returnsum;        }    }    publicstaticlongaddArray(int num){//数组添加计算阶乘        long[]arr=newlong[21];//创建数组        arr[0]=1;                intlast=0;        if(num>=arr.length){            thrownewIllegalArgumentException("传入的值太大");//抛出传入的数太大异常        }        if(num < 0)            thrownewIllegalArgumentException("必须为正整数!");//抛出不合理参数异常        while(last<num){//建立满足小于传入数的while循环            arr[last+1]=arr[last]*(last+1);//进行运算            last++;//last先进行运算,再将last的值加1        }        returnarr[num];    }    publicstaticsynchronizedBigInteger bigNumber(int num){//利用BigInteger类计算阶乘            ArrayList list = newArrayList();//创建集合数组            list.add(BigInteger.valueOf(1));//往数组里添加一个数值            for(inti = list.size(); i <= num; i++) {                BigInteger lastfact = (BigInteger) list.get(i - 1);//获得第一个元素                BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(i));//获得下一个数组                list.add(nextfact);            }            return(BigInteger) list.get(num);//返回数组中的下标为num的值    }    public staticvoidmain(String []args){//java程序的主入口处        intnum=5;        intnum1=23;        System.out.println("简单的循环计算"+num+"的阶乘为"//调用simpleCircle                +simpleCircle(num));        System.out.println("利用递归计算"+num+"的阶乘为"//调用recursion                +recursion(num));        System.out.println("数组添加计算"+num+"的阶乘为"//调用addArray                +addArray(num));        System.out.println("利用BigInteger类计算"+num1+"的阶乘为"//调用bigNumber                +bigNumber(num1));            }}

 

Calculation of four factorial in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.