10323 - Factorial! You Must be Kidding!!!

All about problems in Volume 103. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
User avatar
yahoo
Learning poster
Posts: 93
Joined: Tue Apr 23, 2002 9:55 am

10323 - Factorial! You Must be Kidding!!!

Post by yahoo »

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;
}
10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

Post by 10153EN »

I think you better have a look at a thread in the Real Time Clarification, which is about this problem.

This problem is not as easy and straightforward as you think~
User avatar
yahoo
Learning poster
Posts: 93
Joined: Tue Apr 23, 2002 9:55 am

Post by yahoo »

I can't understand what do you mean by thread in real time clarification. Would you please explain?
10153EN
Experienced poster
Posts: 148
Joined: Sun Jan 06, 2002 2:00 am
Location: Hong Kong
Contact:

Post by 10153EN »

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
Caesum
Experienced poster
Posts: 225
Joined: Fri May 03, 2002 12:14 am
Location: UK
Contact:

Post by Caesum »

this problem is just completely stupid. what is it saying ? that f(-1)=f(0)/0 ???
the factorial function can be defined 'continuously' in terms of an integral - it is called the gamma function. the gamma function has poles at negative integers and is undefined.
LawrenceT
New poster
Posts: 14
Joined: Sun Feb 24, 2002 2:00 am
Location: Singapore
Contact:

Post by LawrenceT »

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:
xenon
Learning poster
Posts: 100
Joined: Fri May 24, 2002 10:35 am
Location: Scheveningen, Holland

Post by xenon »

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
LawrenceT
New poster
Posts: 14
Joined: Sun Feb 24, 2002 2:00 am
Location: Singapore
Contact:

Post by LawrenceT »

yes, i totally agree with you... the problem really has no mathematical basis... but anyway, the thing i stated earlier is just the solution
dh3014
New poster
Posts: 24
Joined: Tue Mar 26, 2002 2:00 am
Location: Taiwan
Contact:

Post by dh3014 »

indeed...by definition
f(-1) = 1 / 0 should be NaN instead of Inf

lim 1 / n is Inf
n->0+
Joe Smith
New poster
Posts: 26
Joined: Wed Apr 17, 2002 5:56 am

Post by Joe Smith »

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
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. :)

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.
..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. »

Someone already writes the answer in ranking.
Try to read the ranking before posting question.......
Fernandez Jose
New poster
Posts: 8
Joined: Wed Sep 18, 2002 2:10 am

Post by Fernandez Jose »

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.
yatsen
Learning poster
Posts: 68
Joined: Fri Nov 23, 2001 2:00 am
Location: taiwan

Post by yatsen »

What is wrong with this easy problem?
Have any trap?
speciale
New poster
Posts: 1
Joined: Mon Sep 30, 2002 6:13 am

10323

Post by speciale »

why its wrong?

#include <stdio.h>

int main ()
{
unsigned n=0;

while ( scanf("%u", &n) != EOF ) {
if (n < 13)
if (n >= 8) {
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]
..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. »

PLEASE READ OTHER POST IN THE FORUM BEFORE POST.
Just a few posts below, there is a long thread talking about this problem.
Post Reply

Return to “Volume 103 (10300-10399)”