Maxcompute factorial Algorithm

Source: Internet
Author: User

A friend asked me a question a few days ago: "How is the factorial of 10000 calculated ?" At that time, I was a little confused. The number "10000" was too large, and no matter what data type was used to save the results, it would overflow. What should I do? At a time, I was helpless. Then it was despised. After receiving a reminder from a friend, I suddenly realized how to implement it. Originally, I used arrays to simulate numbers. No matter how large the number is, the array length can be expressed as long as it is long enough, this method can be used to calculate big data. It looks quite useful. I used itProgramIf it is useful, you can also use it for reference. (At least we can also despise others)
First, define an array that is long enough.
Take the factorial of 10000 as an example. The final result length is 35660 bits, so we can define an array of 40000 members.
Int result [40000];
The core idea is to save the number on each digit of the computing result to a group member, for example:
Save 124 to the array and the Save result should be
Result [0] 4
Result [1] 2
Result [2] 1
This is certainly no problem. An int-type data store a number smaller than 10 will never overflow. But it is a little troublesome to handle.
Think of the entire array as a number. When this number is multiplied by a number, each bit needs to multiply with this multiplier and add the carry of the first one. The calculation method is the same as that in elementary school mathematics. The product of a single digit is the number that should be expressed in the current bit, and carry more than 10 digits. Because the multiplier cannot be greater than 10000, the multiplier will not be greater than 100000 when multiplied by a book smaller than 10, plus the carry of the previous digit to maintain this result with an int data. The statement is as follows:
Int result = Result [x] * multiplier + carry;
The calculation result of each bit is available. Place the single digits of the result to the array element:
Result [x] = Result % 10;
The next step is to calculate the input bit:
Carry = Result/10;
In this way, the entire array is computed by a bit, and there may be carry. In the same way, the carry value is split into a single number and placed into the corresponding array element.
Finally, output the result, and print it again from the highest digit. The complete program is as follows, which is successfully debugged under vc6.0.

 

# Include <iostream> </P> <p> using namespace STD; </P> <p> void main () <br/>{< br/> int result [40000]; // Save the array of settlement results <br/> int Height = 1; // The highest bit of the result <br/> int num; // calculate the factorial number <br/> cout <"input num :"; <br/> // enter a number in the console <br/> CIN> num; <br/> If (Num> 10000 | num <0) <br/>{< br/> cout <"wrong num! "; <Br/> return; <br/>}< br/> result [0] = 1; <br/> for (INT I = 1; I <= num; I ++) <br/>{< br/> int res = 0; // carry <br/> for (Int J = 0; j <peight; j ++) <br/> {<br/> int Buf = Result [J] * I + Res; // calculation result <br/> result [J] = Buf % 10; // get the current bit <br/> res = Buf/10; // calculate carry <br/>}< br/> while (RES) <br/> {<br/> result [height + +] = res % 10; // obtain the current bit <br/> Res/= 10; // calculate carry <br/>}</P> <p> cout <num <"'s factorial is: "<Endl; <br/> // output result <br/> for (int K = height-1; k> = 0; k --) <br/>{< br/> cout <result [k]; <br/>}< br/> cout <Endl; <br/> cout <"length" <peight <Endl; <br/>}< br/>

finished.

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.