Big Number Problem: Calculate the factorial of n and the factorial of n for the big number.

Source: Internet
Author: User

Big Number Problem: Calculate the factorial of n and the factorial of n for the big number.

Question: 100!

This seems to be a simple answer, and recursive solutions are not under pressure.

int func(int n){    if(n <= 1) return 1;    else return n*func(n-1);}
But you will find that the question is really so simple, consider whether the integer data does not cross the border?

This is actually a big number problem!

How to express a large number? It is very direct. We will think of a string to represent it, but we have to perform a factorial operation during the expression process. Is it as complicated as we think?

In fact, we use the basic multiplication thought (from single-digit to high-digit multiplication, carry-in) to multiply each temporary result by a factorial element and carry it to the high-digit, the problem is not that complicated.

The comments in the Code are very detailed, not to mention, and they are directly pasted below.

Public static void main (String [] args) throws IOException {int digit = 1; // number of digits int temp; // The product result int I, j, carry of any element of the factorial and a temporary result; // carry boolean isnavigate = false; // whether the input number is positive or negative. int [] a = new int [3000]; // make sure that the array that saves the final calculation result is large enough. System. out. println ("enter a number and calculate its factorial"); String nStr = ClassicalIOCode. getSystemInput (); int n = 1; try {n = Integer. parseInt (nStr);} catch (NumberFormatException nfe) {System. out. println ("Enter A valid positive integer! "); Return;} if (n <0) {n =-n; isnavigate = true;} a [0] = 1; // initialize the result to 1for (I = 2; I <= n; I ++) {// start factorial, factorial elements are listed in sequence from 2. // they are considered based on the basic multiplication algorithm (from single-digit to high-digit by-digit multiplication and carry-in, multiply each of the temporary results by a factorial element for (j = 1, carry = 0; j <= digit; j ++) {// carry: carry temp = a [j-1] * I + carry; // One of the corresponding factorial is multiplied by one of the current temporary results // (plus carry) a [j-1] = temp % 10; // update the bit information of the temporary result carry = temp/10; // check whether there is a bid} while (carry! = 0) {// If a [++ digit-1] = carry % 10; // Add a new digit and add information. The number of digits increases by 1 carry = carry/10; // you can check whether the carry is supported.} if (isnavigate) {if (n % 2 = 1) {System. out. print (-n) + "factorial:" + (-n) + "! =-"); // Display result} else {System. out. print (-n) +" factorial: "+ (-n) + "! = "); // Display result} else {System. out. print (n +" factorial: "+ n + "! = "); // Display result} for (j = digit; j> = 1; j --) {System. out. print (a [j-1]);} System. out. println ();}



Write a program that can have a factorial (n) with a large number of n. The value range of n is changed from 1.

The classic algorithm used to calculate the factorial has long been available. The following changes have been made as required. I hope they will help you:
# Include <stdio. h>
Int main ()
{
Int n;
Int k;
Int a [9000]; // make sure that the array that saves the final calculation result is large enough
Int digit = 1; // number of digits
Int temp; // the product of any element of a factorial and a certain value of a temporary result
Int I, j, carry; // carry

Printf ("please in put n: \ n ");
Scanf ("% d", & n );
A [0] = 1; // initialize the result to 1 first

For (I = 2; I <= n; I ++) {// start the factorial. The factorial elements start from 2 and start to "debut"
// Multiply each of the temporary results by the factorial element based on the basic multiplication algorithm.
For (j = 1, carry = 0; j <= digit; j ++) {// carry: carry
Temp = a [J-1] * I + carry; // One of the corresponding factorial is multiplied by one of the current temporary results // (plus carry)
A [J-1] = temp % 10; // update the bitwise information of the temporary result
Carry = temp/10; // check whether there is an advance
}
While (carry) {// if there is an increment
A [++ digit-1] = carry % 10; // Add a new digit to add information. Increase by 1
Carry = carry/10; // you can check whether it can be carried.
}
}
Printf ("n! = "); // Display the result
K = 0;
For (j = digit; j> = 1; j --){
If k = 3
{
Printf (",");
K = 0;
}
Else
K ++;
Printf ("% d", a [J-1]);
}
Printf ("\ n ");
Return 0;
}

Write a C ++ program, which can be a factorial (n!) with a large number of n !), The value range of n changes from 1100.

# Include <iostream>
# Include <vector>

Using namespace std;

# Define MAX_DIGITAL 10000

Void PrintFactorial (unsigned int n)
{
Vector <unsigned int> r (1, 1 );
Size_t j = 0;

For (unsigned int I = 1; I <= n; I ++)
{
For (j = 0; j <r. size (); j ++)
{
R [j] * = I;
}

Int carry = 0;
J = 0;

Do
{
R [j] + = carry;
Carry = r [j]/MAX_DIGITAL;
R [j] % = MAX_DIGITAL;
} While (++ j <r. size ());
If (carry> 0)
{
R. push_back (carry );
}
}

Cout <* r. rbegin ();
For_each (++ r. rbegin (), r. rend (), [] (const long val ){
Printf_s (", % 04d", val );
});
}

Int main (int argc, _ TCHAR * argv [])
{
PrintFactorial (100 );
Cout <endl;
System ("pause ");
Return 0;
}

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.