The big data factorial algorithm has many specific implementations on the Internet. I also found an algorithm idea by the way.

Source: Internet
Author: User

First, define two integer Arrays:
Int FAC [1000]; // set to 1000 bits for the moment. I call it a "result array"

Int add [1000]; // I call it a carry array"

The functions of the two arrays are described as follows:

1. FAC [1, 1000]

For example, if the factorial of a number 5 is 120, I will use this array to store it:
FAC [0] = 0
FAC [1] = 2
FAC [2] = 1

Now I understand the role of the array FAC. With such an array, we can put the factorial and the result is a number of 1000 bits.

2. Before introducing add [1000], I will introduce the idea of algorithms! For example:
We learned 5 from above! How is it stored.

At 5! 6 !, The demo is as follows:

FAC [0] = FAC [0] * 6 = 0
FAC [1] = FAC [1] * 6 = 12

FAC [2] = FAC [2] * 6 = 6

3. Now we use "carry array" add [1000].

Note: add is the carry value of the I-bit to the I + 1 in the result calculated in step 1. Or the following example:
Add [0] = 0;

Add [1] = 1; // calculation process: (FAC [1] + Add [0]) % 10 = 1

Add [2] = 0; // calculation process: (FAC [2] + Add [1]) % 10 = 0
.......
.......

.......

Add [I + 1] = (FAC [I + 1] + Add) % 10

Now we know what our array add [] is, and how to obtain the ADD.

4. Now that we know the value of add [1000], we can calculate the final result based on steps 1 and 3. (Step 1 is only a step we understand. The following calculation already contains it)

FAC [0] = (FAC [0] * 6) mod 10 = 0

FAC [1] = (FAC [1] * 6 + Add [0]) mod 10 = 2

FAC [2] = (FAC [2] * 6 + Add [1]) mod 10 = 7

Now we have calculated 6 !. Then, you can output the number in the array FAC [1000] in characters to the screen.

5. Another note is that we need a variable to record the number of actually used bits in FAC [1000], for example, 5! The first three digits are used;
We define it as top

In order to calculate the top, we use add [1000], or the above example:


5! Top = 3. Let's look at 6 on this basis! Top =?
Because add [2] = 0

So Top = 3 (not changed)
That is to say, if the highest bit has an increment, our top = Top + 1; otherwise, the top value remains unchanged.

6. To sum up, we can find that we convert the factorial into two multiplication methods of numbers within 10, and there are also two families of numbers within 10.

Therefore, no matter how big the number is, it can basically be calculated, as long as your array is large enough.

 

From: http://www.chinaunix.net/jh/23/312721.html


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.