[Algorithm C language description] factorial algorithms for large numbers and huge numbers

Source: Internet
Author: User

The array method is used to solve the problem that the factorial results of large numbers and huge numbers are out of bounds.

The specific algorithms have the simplest Multiplication operation ideas.

# Include <stdio. h>

Int main ()
{
 
Int N; // factorial size
Printf ("Enter n size :");
Scanf ("% d", & N); // receives the factorial size from the keyboard
Int A [200]; // make sure that the array that saves the final calculation result is large enough
Int carry; // carry
Int digit = 1; // number of digits
A [0] = 1; // initialize the result to 1 first
Int temp; // the product of any element of a factorial and a certain value of a temporary result

For (INT I = 2; I <= N; ++ I) // start the factorial. The factorial element starts to appear in sequence from 2"
{
// Multiply each of the temporary results by the factorial element based on the basic multiplication algorithm.
For (Int J = 1, carry = 0; j <= digit; ++ J)
{
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/= 10; // you can check whether the carry can be carried.
}
}

Printf ("Result:/n % d! = ", N); // display the result
For (INT I = digit; I> = 1; -- I)
{
Printf ("% d", a [I-1]);
}
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.