Nyoj38 Big Data factorial

Source: Internet
Author: User

Source: http://hi.baidu.com/shiranon/item/96a611111ad1667b71d5e8a1

1. Recursive Methods

This is the easiest way to think about it. If it is a factorial of 1, 1 is returned, and all others return the factorial of N-1 and the product of N, which can be called cyclically. However, the problem is that double is used to store the value. Because of the precision of double and the number size that can be stored, the factorial of A large number cannot be calculated.
Ii. Array Method
Idea: Use a data array to store each digit of a factorial. First, set the first digit to 1 and the number to 1. Then, store the product of each multiplication back to the array, and cyclically process the number of more than 10 in each array. If the number is greater than 10, carry the number, and Add 1 to the number. The original number is divided by 10, after adding the values of the previous digit, the Business digit is stored in the array of the previous digit, and the remainder is stored in the array of the original digit.
For example, 5! Value
Step 1:
1! = 1
Number of digits 1
Array content 0 0 0 1
Step 2:
2! = 2*1! = 2
Number of digits 1
Array content 0 0 0 2
Step 3:
3! = 3*2! = 3*2 = 6
Number of digits 1
Array content 0 0 0 6
Step 4:
4! = 4*3! = 4*4 = 24
Number of digits 1
Array content 0 0 0 24
Because 24 is greater than 10, carry is required
Data [1] = data [1] + data [0]/10 = 0 + 2 = 2
Data [0] = data [0] % 10 = 4
So the array content is 0 0 2 4
Number of digits 2
Step 5:
5! = 5*4! = 5*24 = 120
Number of digits 2
The array content is 0 0 2*5 4*5
0 0 10 20
Because data [0] is greater than 10, carry is required
Data [1] = data [1] + data [0]/10 = 10 + 2 = 12
Data [0] = data [1] % 10 = 0
The array content is 0 0 12 0.
Data [2] = data [2] + data [1]/10 = 0 + 1 = 1
Data [1] = data [1] % 10 = 2
Number of digits plus 1

The array content is 0 1 2 0

AC code:

# Include <iostream> # include <cstring> using namespace STD; int main () {int data [16325]; int digit; int I, j, R, K; int N; for (I = 1; I <= 16325; I ++) data [I] = 0; data [0] = 1; data [1] = 1; digit = 1; cin> N; for (I = 1; I <= N; I ++) // factorial {for (j = 1; j <= digit; j ++) // Multiply Data [J] = data [J] * I; for (j = 1; j <= digit; ++ J) // a value that exceeds the value, if (data [J]> 10) {for (r = 1; r <= digit; r ++) {If (data [digit]> 9) digit ++; // carry, plus one digit data [R + 1] = data [R + 1] + data [R]/10; // base + carry data [R] = data [R] % 10; // perform remainder }}for (k = digit; k> 0; k --) {cout <data [k] ;}cout <Endl; return 0 ;}
Related Article

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.