Questions in minutes n! What is the test?

Source: Internet
Author: User

About n! Every programmer can do this in minutes.

Method 1: the simplest method is recursive multiplication:

The Code is as follows. Many input parameters are added to main to judge the input: the input must be a number:

package test.ms;public class TestJC {   public static void main(String[] args) {   try{  if(args == null || args.length >1 || args.length == 0){ System.out.println("Please input  one  number");  }else{  int  n = Integer.parseInt(args[0]);  factorial(n);  }    }catch(NumberFormatException e){  e.printStackTrace();  System.out.println("Please  intput  number  formate ");  }  }   public  static  void  factorial(int  n){   int   result = 1;  for(int i = n; i > 0; i--){  result = result*i;  }  System.out.println(result);   }}

The above is the code for Recursive multiplication, but there is a serious problem, that is, the range that int can represent: view the official tutorial as follows:

INT:intData type is a 32-bit signed two's Complement
Integer. it has a minimum value of-2,147,483,648 and a maximum value of 2,147,483,647 (inclusive ). for integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. this data type will
Most likely be large enough for the numbers your program will use, but if you need a wider range of values, uselongInstead.

The maximum value of the int type indicates that the positive number is 2,147,483,647. When the input value is 1-12, the value can be calculated smoothly,

12! Is 479001600

13! No.

How can we write a general method for calculating the factorial:


The biginteger in Java is used to calculate the factorial.

An integer of any precision that cannot be changed by biginteger. In all operations, biginteger (such as the basic integer type of Java) is expressed in the form of binary complement ). Biginteger
Provides the counterparts of all basic integer operators in Java and all related methods of Java. Lang. Math. Biginteger also provides the following operations: modulo arithmetic, gcd calculation, prime number testing, prime number generation, bit operations, and some other operations.

Use biginteger for calculation:

package test.ms;import java.math.BigInteger;public class TestJC {   public static void main(String[] args) {   try{  if(args == null || args.length >1 || args.length == 0){ System.out.println("Please input  one  number");  }else{  int  n = Integer.parseInt(args[0]);  System.out.println(n);  factorial2(n);  }    }catch(NumberFormatException e){  e.printStackTrace();  System.out.println("Please  intput  number  formate ");  }  }           public  static  void  factorial2(int  n){   BigInteger  result = new  BigInteger("1");   for(int i=n ; i > 0 ; i--){   BigInteger  temp = new  BigInteger(String.valueOf(i));    result = result.multiply(temp);   }   System.out.println(result);      }}

Use the above Code 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Accurate Computation;


This method is too simple. It only examines the value range of the basic data type INT:

Method 2: Recursive Implementation

In general, Recursive Implementation is required in the interview questions, and then the recursion is investigated:

Recursion is simple. Generally, programmers can do it in minutes:

The common method code is as follows:

package test.ms;public class TestFactorialRecursion {  public static void main(String[] args) {   try{  if(args == null || args.length >1 || args.length == 0){ System.out.println("Please input  one  number");  }else{  int  n = Integer.parseInt(args[0]); int  result = factorial(n); System.out.println(result);  }    }catch(NumberFormatException e){  e.printStackTrace();  System.out.println("Please  intput  number  formate ");  }  }    public  static  int   factorial(int  n){  if(n == 1){  return  1;  }else{  return   n*factorial(n-1);  }  }}

The above Code uses Recursion to implement n !, However, due to the limitations of the int type range, the above Code can only calculate the factorial of 1--12:

If you want to calculate the factorial of any given integer, replace the above Code with the biginteger format:

The code for all operations using biginteger is as follows:

package test.ms;import java.math.BigInteger;public class TestFactorialRecursion {  public static void main(String[] args) {   try{  if(args == null || args.length >1 || args.length == 0){ System.out.println("Please input  one  number");  }else{  int  n = Integer.parseInt(args[0]);  BigInteger   m = new BigInteger(String.valueOf(n)); BigInteger  result = factorial2(m); System.out.println(result);  }    }catch(NumberFormatException e){  e.printStackTrace();  System.out.println("Please  intput  number  formate ");  }  }  public  static  BigInteger  factorial2(BigInteger  n){  BigInteger   value1 = new  BigInteger("1");  if( value1.compareTo(n) == 0){  return   value1;  }else{  BigInteger  temp = new  BigInteger(String.valueOf(n));  return   temp.multiply(factorial2(temp.subtract(value1)));  }    }}

Because recursion is used, the above input parameters and return types both adopt the biginteger type:

Biginteger provides some basic operations such as addition, subtraction, multiplication, and division of biginteger:

In the above example:

UsedMultiply(Biginteger
Val) andsubtract(BigInteger val)Used for multiplication and Subtraction

Input

100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

It can still be accurately calculated.

This mainly describes the range of recursion and basic type values.



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.