Solving notes (38) -- big integer factorial calculation

Source: Internet
Author: User

Problem description: Calculate the factorial of an integer N, 0 <= n <= 5000.

For example, if n is 50, the result is 30414093201713378043612608166064768844377641568960512000000000000.

 
Idea: Starting from the factorial definition, n! = N * (n-1) * (n-2) *... * 2*1. Because N is large, basic data types cannot be stored. However, we can use an integer array to store results (including intermediate results). Each element of the array stores one of the results, and the first element of the array is initialized to 1. The calculation process is relatively simple, N, n-1, N-2, 1 multiplied by the current value, that is, the value of the integer array to save. The final result is printed.

 
Reference code:

Void caln_solution1 (int n) {int A [20000] = {1, 0}; // Save the result, including the intermediate result int m = 0; For (; n --) // multiply by N, n-1, 1 {int C = 0; // carry value for (INT I = 0; I <= m; I ++) // multiply the intermediate result by N {C + = A [I] * n; // calculate the result first and then consider carrying a [I] = C % 10; // The intermediate result of the I-th digit C/= 10; // carry-up} For (; C/= 10) // carry a [++ m] = C % 10;} For (; m> = 0; m --) // print the result cout <A [m]; cout <Endl ;}

In the above program, each element of the integer array represents only one bit of the result, which wastes a lot of space. The improved method is to let each element in the array Save the four digits of the result, which can greatly save space. The code is modified as follows.

Void caln_solution2 (int n) {int A [5000] = {1, 0}; // Save the result, including the intermediate result int m = 0; For (; n --) // multiply N, n-1, 1 {int C = 0, I = 0; For (; I <= m; I ++) // multiply the intermediate result by N {C + = A [I] * n; // calculate the result first and then consider carrying a [I] = C % 10000; // The intermediate result of the I-th digit. Note that the modulo 10000 is used to indicate the 4-digit C/= 10000; // carry up} A [M + 1] = C; // carry a maximum of 1 bits, so you do not need to use the loop if (C> 0) m ++;} cout <A [M --]; for (; m> = 0; M --) cout <SETW (4) <setfill ('0') <A [m]; cout <Endl ;}

The Code is as follows.

#include<iostream>#include<iomanip>using namespace std;int main(){int n;cin>>n;CalN_Solution1(n);CalN_Solution2(n);return 0;} 

 
I enjoy the copyright of blog articles, reprint please indicate the source http://blog.csdn.net/wuzhekai1985

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.