You are not logged in. Please login at www.codechef.com to post your questions!

×

Small Factorial

//my code for smallfactorial,it's giving correct output in my system but not here..please tell me whats wrong in it???

#include <stdio.h>
#include <stdlib.h>
int fact(int);

int main()
{
    int t,n,r;
    scanf("%d",&t);
    while(t--)
    {
        r=0;
        scanf("%d",&n);
        r=fact(n);
        printf("%d\n",r);
    }
    return 0;
}
int fact(int n)
{

     if(n==1)
        return 1;
    else
        return(n*fact(n-1));
}

asked 06 Feb '14, 21:14

shivali24's gravatar image

2★shivali24
14122
accept rate: 0%

edited 06 Feb '14, 21:22

kunal361's gravatar image

4★kunal361
6.0k133272


it will not work for larger inputs...by larger i mean nos like 20-30 will also give a WA...as integer will overflow!!!

try putting 100...the worst case!!!

link

answered 06 Feb '14, 21:23

kunal361's gravatar image

4★kunal361
6.0k133272
accept rate: 21%

edited 06 Feb '14, 21:24

@kunal361- thanks for your ans,u r ri8 but i used unsigned long integer variable for storing the result but it is still not working...

link

answered 06 Feb '14, 21:30

shivali24's gravatar image

2★shivali24
14122
accept rate: 0%

shivali24: Just think about it for a while. What's the range of int? Approximately 10^9. What's the range of long long? Approximately 10^18. How large can the result get? Try a calculator: 30! is around 10^32. Well... shit.

Of course it won't work with any integer type of C/C++. Try using "bigint"s - represent a number as an array of base-10 digits. (It works a bit faster if you use a larger base.) You just need to implement the addition and multiplication of such numbers, which is easy enough in O(number of digits ^2) in the same way as you'd do it on paper.

link

answered 06 Feb '14, 22:07

xellos0's gravatar image

7★xellos0
5.9k54393
accept rate: 10%

edited 06 Feb '14, 22:10

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×196
×33

question asked: 06 Feb '14, 21:14

question was seen: 961 times

last updated: 06 Feb '14, 22:10