Large number factorial

Source: Internet
Author: User

Description We all know how to calculate the factorial of a number, but if the number is large, how do we calculate it and output it?

Input: Enter an integer m (0<m<=5000)
Output: The factorial of the output m, and enter a newline character after the output ends
Example input: 50
Sample output: 30414093201713378043612608166064768844377641568960512000000000000
Bo Main analysis: The subject seems to be very simple, the operation of a little trouble, but also to pay attention to some details ·, because the factorial of 5000 is too large, so can not be used as a number of output, a similar conversion into the system,
Here I used 10000 binary, with the array to represent each bit of the number, it is easy to deal with, and pay attention to where the factorial to terminate,
Reference code:
    
#include <stdio.h>
#include <math.h>
int a[6800];
int Wu (int j)//Determine the number of arrays to be used
{
int i,s;s=j;
For (i=0;i<1000;i++)
if (a[j+i+1]!=0)
Break ;
if (i==1000)
return (0);
else return (1);

}
int bei (int n,int j,int c)//calculate each one with the recursive principle
{
int m,i,k;
A[j]=a[j]*n+c;
c=a[j]/10000;
a[j]=a[j]%10000;
if (Wu (j) | | | c!=0)
return (bei (N,j+1,c));
else Return (j);
}
Main ()
{
int i,j,k,m;
scanf ("%d", &m);
a[0]=1;
For (i=1;i<=m;i++)
K=bei (i,0,0);
printf ("%d", a[k]);
for (i=k-1;i>=0;i--)//output to the corresponding complement 0
{if (a[i]<10000&&a[i]>999)
printf ("%d", A[i]);
else if (a[i]<1000&&a[i]>99)
printf ("0%d", A[i]);
else if (a[i]<100&&a[i]>9)
printf ("00%d", A[i]);
Else
printf ("000%d", A[i]);}
Putchar (' \ n ');



}


Large number factorial

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.