10323 - Factorial! You Must be Kidding!!!
Moderator: Board moderators
10323 - Factorial! You Must be Kidding!!!
The problem seemed to be very much easy to me. But i can't understand why i am getting wrong answers. Can any body help me with some typical input.Here is my code:
#include<stdio.h>
#include<math.h>
#include<string.h>
main()
{
int i,r,j,n;
double x,v;
char a[10],b[10];
while(1)
{
n=0;
r=scanf("%s",a);
if(r==EOF) break;
for(i=0;i<strlen(a);i++)
if(a>='1' && a<='9')
break;
n=0;
for(j=i;j<strlen(a);j++)
b[n++]=a[j];
v=0;
for(i=0;i<n;i++)
v=v+(b-'0')*pow(10,n-i-1);
if(v<8) printf("Underflow!\n");
else if(v>13) printf("Overflow!\n");
else
{
x=1;
for(i=2;i<=v;i++)
x=x*i;
printf("%.0lf\n",x);
}
}
return 0;
}
#include<stdio.h>
#include<math.h>
#include<string.h>
main()
{
int i,r,j,n;
double x,v;
char a[10],b[10];
while(1)
{
n=0;
r=scanf("%s",a);
if(r==EOF) break;
for(i=0;i<strlen(a);i++)
if(a>='1' && a<='9')
break;
n=0;
for(j=i;j<strlen(a);j++)
b[n++]=a[j];
v=0;
for(i=0;i<n;i++)
v=v+(b-'0')*pow(10,n-i-1);
if(v<8) printf("Underflow!\n");
else if(v>13) printf("Overflow!\n");
else
{
x=1;
for(i=2;i<=v;i++)
x=x*i;
printf("%.0lf\n",x);
}
}
return 0;
}
Quoted from the words of the problem setter:
When I did mathematics a lot factorial of negative numbers was a very interesting issue to me. If factorial(-1) is not infinity then why is
C(4,5) = 4!/((5!)(-1!)) = 0, have you ever thought it this way. I am getting many complains on this issue, some saying it is non standard, I ask u to think again as f(n)=n*f(n-1) can be written as f(n-1)=f(n)/n.
And the problem statement clearly says than you can manipulate that expression.
-Shahriar Manzoor
well... i guess the answers are quite obvious for positive input... well... when input is -1, f(-1) = f(0) / 0 = 1 / 0 = positive infinity (overflow)... den when u divide that by a negative num... u get another answer... and when u divide that by another negative num... what do you get?... that's the general idea... don't wanna give away too much ![:wink:](./images/smilies/icon_wink.gif)
![:wink:](./images/smilies/icon_wink.gif)
f(-1) is just undefined, period. It's not infinity, gogolplex, or anything; just undefined, period. That's because the factorial function f(n) is only defined for n={0,1,2,3,...}.
The reasoning that f(-1) should be infinity, because C(4,5) should be 0 is faulty, because C(4,5) is undefined, not zero. It is impossible to pick 5 things out of 4, and that's something else then saying that there are zero ways.
All attempts to assign values to f(n) for negative n, are stupid layman's tricks, based on dividing out zeroes, manipulating infinity, etc. As stated before: f(n) simply doesn't exist for negative n.
A very angry,
-xenon
The reasoning that f(-1) should be infinity, because C(4,5) should be 0 is faulty, because C(4,5) is undefined, not zero. It is impossible to pick 5 things out of 4, and that's something else then saying that there are zero ways.
All attempts to assign values to f(n) for negative n, are stupid layman's tricks, based on dividing out zeroes, manipulating infinity, etc. As stated before: f(n) simply doesn't exist for negative n.
A very angry,
-xenon
That depends on your definition of C(n,k). If you take C(n,k) to be the coefficient of x^k in (1+x)^n, which is usually the definition I assume, then C(4,5) is indeed 0. And since choosing 5 things out of 4 can't be done, clearly the number of ways of doing so is not undefined, but 0. On the other hand, choosing -1 things out of 4, that would be undefined I suppose.xenon wrote:f(-1) is just undefined, period. It's not infinity, gogolplex, or anything; just undefined, period. That's because the factorial function f(n) is only defined for n={0,1,2,3,...}.
The reasoning that f(-1) should be infinity, because C(4,5) should be 0 is faulty, because C(4,5) is undefined, not zero. It is impossible to pick 5 things out of 4, and that's something else then saying that there are zero ways.
All attempts to assign values to f(n) for negative n, are stupid layman's tricks, based on dividing out zeroes, manipulating infinity, etc. As stated before: f(n) simply doesn't exist for negative n.
A very angry,
-xenon
![:)](./images/smilies/icon_smile.gif)
Anyway, bringing C(n,k) has NOTHING to do with the definition of n! so I don't even know why it was mentioned... that just makes Shahriar's explanation make even less sense. The value of C(4,5) has nothing to do with how f(-1) is defined, it's just 0.
-
- New poster
- Posts: 8
- Joined: Wed Sep 18, 2002 2:10 am
well now i'm discovering a new impresive mathematics.
As it was said you can handle this problem in this way: f(n)=nf(n-1)=> f(n-1)=f(n)/n, lets see.
So to handle f(-1) we do f(0)=0f(-1),(this will always be cero, if f(-1)#oo).
As is defined f(0)=1
then f(-1)=1/0=+oo
this solution must verify de ecuation, as result
f(0)=1=0*oo (this is a great discovering for me)
In any case if the factorial function is named then it sould be a factorial not a extraneus function to puzzle people.
I'm for to change the name of the problem.
As it was said you can handle this problem in this way: f(n)=nf(n-1)=> f(n-1)=f(n)/n, lets see.
So to handle f(-1) we do f(0)=0f(-1),(this will always be cero, if f(-1)#oo).
As is defined f(0)=1
then f(-1)=1/0=+oo
this solution must verify de ecuation, as result
f(0)=1=0*oo (this is a great discovering for me)
In any case if the factorial function is named then it sould be a factorial not a extraneus function to puzzle people.
I'm for to change the name of the problem.
10323
why its wrong?
#include <stdio.h>
int main ()
{
unsigned n=0;
while ( scanf("%u", &n) != EOF ) {
if (n < 13)
if (n >=
{
switch (n) {
case 8: printf("%lu\n",40320); break;
case 9: printf("%lu\n",362880); break;
case 10: printf("%lu\n",3628800); break;
case 11: printf("%lu\n",39916800); break;
case 12: printf("%lu\n",479001600); break;
/* case 13: printf("%lu\n",6227020800); break; */
}
}
else printf("Underflow!\n");
else printf("Overflow!\n");
}
return 0;
}[/c]
#include <stdio.h>
int main ()
{
unsigned n=0;
while ( scanf("%u", &n) != EOF ) {
if (n < 13)
if (n >=
![8)](./images/smilies/icon_cool.gif)
switch (n) {
case 8: printf("%lu\n",40320); break;
case 9: printf("%lu\n",362880); break;
case 10: printf("%lu\n",3628800); break;
case 11: printf("%lu\n",39916800); break;
case 12: printf("%lu\n",479001600); break;
/* case 13: printf("%lu\n",6227020800); break; */
}
}
else printf("Underflow!\n");
else printf("Overflow!\n");
}
return 0;
}[/c]