#转 large number factorial algorithm

Source: Internet
Author: User

Large number factorial algorithm

Large number factorial algorithm

A few days ago my friend asked me a question: "How does the factorial of 10000 count?" "I was a little confused at the time," 10000 "the number is too large, regardless of what data type to save the results will overflow. What can I do about it? At a time helpless. And then despised by a meal. Later by friends reminded, only to realize, finally know how to achieve, the original is to use an array to simulate the number, so no matter how large the number of results, as long as the length of the array can be expressed, with this method can be large data operation. It seems to be quite useful. I use it to implement the program, if useful to the place can also draw on. (At least you can take it to despise others ^_^)
First, define a long enough array.
Taking the factorial of 10000 as an example, the final result is 35,660 bits in length, so we define an array of 40,000 members.
int result[40000];
The core idea is to save the numbers on each of the calculation results to an array member, for example:
Save the 124 to the array, and the result should be
RESULT[0] 4
RESULT[1] 2
RESULT[2] 1
This certainly is no problem, an int type data holds a number less than 10 is absolutely not overflow. But it's a little tricky to deal with.
The whole array is treated as a number, and when multiplied by a number, it is necessary to multiply each bit with the multiplier and add the previous carry. The arithmetic is the same as the primary mathematics, the product of the bits is the current position should be represented by the number, more than 10 need to carry. Because the multiplier cannot be greater than 10000, the multiplier is no more than 100000 when multiplied by a book that is less than 10, plus the previous digit has no problem keeping the result with an int type of data. The wording is as follows:
int result = result[x] * multiplier + rounding;
Each bit is computed, and the single digit of the result is taken out onto the array element:
RESULT[X] = result%10;
The next task is to calculate the rounding:
Rounding = result/10;
So one of them computes the entire array once, and finally possibly the carry, and in the same way, splits the value of the carry into a single number and puts it into the corresponding array element.
Finally output the results, from the highest bit of the digital printing once again OK. The complete program is as follows and is debugged under VC6.0.

1#include <stdio.h>2 intMain ()3 {4     intcarry,n,j;5     inta[40001];6     intDigit;7     intTemp,i; 8      while(SCANF ("%d", &n)! =EOF) {9a[0]=1;d igit=1;Ten      for(i=2; i<=n; i++) One     { A          for(carry=0, j=1; j<=digit; ++j) -         { -temp=a[j-1]*i+carry; thea[j-1]=temp%Ten; -carry=temp/Ten; -         } -          while(Carry) +         { -             //digit++; +a[++digit-1]=carry%Ten; ACarry/=Ten; at         } -     } -     -      for(intK=digit; k>=1; --k) -printf"%d", a[k-1]); -printf"\ n"); inprintf"length=%d\n", digit); -  } to     return 0; +}

#转 large number factorial algorithm

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.