rev='made'/> name='keywords'/> name='author'/> ANURANAN: UVa Solution 10323 - Factorial! You Must be Kidding!!!

Welcome Note

Hi,
I'm Anisuzzaman Babla . I am a full stack Android Application Developer. I am working with Android for more than two years. I have completed my graduation from IIT, Jahangirnagar University. I have developed a series of commercially successful apps on Android. Moreover, 10+ of my Apps are live at Google play store. .
Thank You

UVa Solution 10323 - Factorial! You Must be Kidding!!!

#include<stdio.h>
int main()
{
long long n;
while(scanf("%lld",&n)==1)
{
if(n<0&&n%2==0) printf("Underflow!\n");
else if(n<0&&n%2!=0) printf("Overflow!\n");
else if(n==0||n<=7) printf("Underflow!\n");
else if(n==8) printf("40320\n");
else if(n==9) printf("362880\n");
else if(n==10) printf("3628800\n");
else if(n==11) printf("39916800\n");
else if(n==12) printf("479001600\n");
else if(n==13) printf("6227020800\n");
else if(n>13) printf("Overflow!\n");
}
return 0;
}

3 comments:

  1. can u plz explain this part ?
    if(n<0&&n%2==0) printf("Underflow!\n");
    else if(n<0&&n%2!=0) printf("Overflow!\n");

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  2. For this problem, we have F(n)=n*F(n-1), and F(0)=1. With some manipulations, for negative factorials, we can get: F(0)=0*F(-1), or F(-1)={\frac {F(0)}{0}}=\infty . Continuing on this logic: F(-1)=-1*F(-2), or F(-2)=-F(-1). Similarly, {\frac {F(-1)}{-1}}=F(-3)=-F(-2).

    To ignore all the non-sense, if the input is between 8 to 13, print n!. It's always overflow if n\geq 14, and always underflow from 0 to 7. For negative numbers, negative odd numbers are overflows, and negative even numbers are underflows.

    for details:
    http://www.algorithmist.com/index.php/UVa_10323

    ReplyDelete