## 10323 - Factorial! You Must be Kidding!!!

Moderator: Board moderators

### Re: Give some explanation

razibcse wrote:I can't understand how to calculate factorial of negative numbers...if u put n=0 in the formula (n-1)!=n!/n, u get
-1!=0!/0...how is this possible?

pls give some explanation of the process how negative even numbers get Underflow! and odd numbers get Overflow!...

Fact(0) = 1, But according Fact(n) = n * Fact(n - 1);
Fact(0) = 1 = 0 * Fact(-1)
So Fact (-1) is equal to "unlimited large" (Sorry, my English is poor )

We can also now Fact(-2) is "unlimited small"
(By Fact(-1) = -1 * Fact(-2) )

I think this is the specail case for this problem.

GoOd Luck
InOutMoTo
New poster

Posts: 18
Joined: Sun Aug 10, 2003 12:47 pm

I think you don't consider one situation

that is if the input number is negative

and if the negative number is even, you should print "Underflow"

and if the negative number is odd, you should printf "Overflow"

if you want to know why?

look this rule

Factorial (n) = n*factorial (n-1).

you will find it.
DD
Experienced poster

Posts: 145
Joined: Thu Aug 14, 2003 8:42 am
Location: Mountain View, California

But still I'm quite sure that negative factorial is undefined, has no answer (it's like division by zero). Negative factorial can't be solved in any ways known to mathematics. Besides there's a thread from Shahriar Manzoor telling us not ot bother with this prob. He also said the prob will be redefined.
Joseph Kurniawan
Experienced poster

Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

According to http://mathworld.wolfram.com/Factorial.html...
... when n is a negative integer, in which case n! is equal to complex infinity.

Complex infinity is defined as:
An infinite number in the complex plane whose complex argument is unknown.

which as I understand it, is not exactly undefined as division by zero, but for our purposes in the "real plane world", I guess it is.

UFP2161
A great helper

Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm

This is complete functional solution of this problem.
[c]
int main() {
long long p=1;
const long long o = 6227020800LL;
int i=0;

while(scanf("%i", &i) == 1){
if (i<0) {
if (-i%2==0) {
printf("Underflow!\n");
continue;
} else {
printf("Overflow!\n");
continue;
}
}
p=1;

for(; i>1; i--){
p*=i;
if (p>o) {
break;
}
}
if (p<10000) printf("Underflow!\n");
else if (p>o) printf("Overflow!\n");
else printf("%lli\n", p);
}
return 0;
}
[/c]
romankrejci
New poster

Posts: 1
Joined: Sun Oct 12, 2003 6:53 pm
Location: Czech republic

### 10323 help me

first i think its very easy.now i see its not so easy.i dont know what i have to do now.plz help me

/* @JUDGE_ID: xxxxxx 10323 C++ */

#include<string.h>
#include<math.h>
#include<stdio.h>
char t[1000];
static int sum,n;
long double sum1=0;
factorial(int k)

{
int i=0,c=0,sum,p;
p=strlen(t);
while(i<p)
{
sum=c+(t[i]-48)*k;
t[i]=(fmod(sum,10))+48;
i++;
c=sum/10;
}
while(c>0)
{
t[i++]=(c%10)+48;
c=c/10;
}
t[i]='\0';
}
pr()
{
int i,j;
i=strlen(t);
if(i>10)
printf("Overflow!\n");
else if(i<4)
printf("Underflow!\n");
else
{
for(i=i-1;i>=0;i--)
{
sum1+=(t[i]-48)*pow(10,i);
}
if(sum1>6227020800)
printf("Overflow!\n");
if(sum1<10000)
printf("Uuderflow!\n");
else
printf("%.0Lf\n",sum1);
}
sum1=0;
}

main()
{
int i;
while(scanf("%d",&n)!=-1)
{
strcpy(t,"1");
for(i=2;i<=n;i++)
{
factorial(i);
}
pr();
}
}
problem
New poster

Posts: 27
Joined: Mon Nov 10, 2003 12:40 am

From a quick glance at your program, I can tell that it can't handle negative input. Perhaps you should see previous posts about this prob.
Good luck!!!
There are 3 things one need to be successful : motivation, ability and chance. And if you're a believer, make it four : God's will.
Joseph Kurniawan
Experienced poster

Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

### Re: 10323 help me

problem wrote:first i think its very easy.now i see its not so easy.i dont know what i have to do now.plz help me
...
if(sum1>6227020800)
printf("Overflow!\n");
if(sum1<10000)
printf("Uuderflow!\n"); =>missed something here??
...

And like Joseph say...
for negative input you don't need to find the result..
just check if it's odd or even.
The output should be Overflow if it's odd and Underflow if it's even.
Good Luck..
Regard,
Andre
Bug!
New poster

Posts: 24
Joined: Thu Oct 30, 2003 10:19 am

Thank you,

First I thought that if the number is negative , whether it is even I should output the factorial of its absolute value (|n|!) and otherwise - the negative factorial of its absolute value (-|n|!)
By the way , I have never heard of the factorial of a negative numer.
Thank you one more time
kiha
kiha
New poster

Posts: 37
Joined: Sat Dec 20, 2003 10:59 pm

### Thanx

Thx Bug!
I was really frustrated coz factorial of negative number .. Thx

Aakash
...I was born to code...
aakash_mandhar
New poster

Posts: 38
Joined: Thu Dec 11, 2003 3:40 pm
Location: Bangalore

### 10323 compiler error!

can somebody tell me why it will compiler error?
[cpp]
#include <iostream>
using namespace std;

int main()
{
long long d;
int n;
int i;
bool f;
while(cin>>n)
{
f=false;
d=1;
for(i=1;i<=n;i++)
{
d*=i;
if(d>6227020800)
{
f=true;
break;
}
}
if(f)
cout<<"Overflow!"<<endl;
if(d<10000&&!f)
cout<<"Underflow!"<<endl;
if(d>=10000&&d<=6227020800&&!f)
cout<<d<<endl;
}

return 0;
}
[/cpp]
oulongbin
Learning poster

Posts: 53
Joined: Sat Jul 10, 2004 5:57 pm
Location: Shanghai China

if(d>6227020800)

The problem could be over here. You are not allowed to access constant integer as big as this. You can use double to replace this.

sohel
Guru

Posts: 862
Joined: Thu Jan 30, 2003 5:50 am
Location: University of Texas at San Antonio

place ll after each number, that is

[cpp]

if(d>6227020800ll)
{
f=true;
break;
}
}
if(f)
cout<<"Overflow!"<<endl;
if(d<10000ll&&!f)
cout<<"Underflow!"<<endl;
if(d>=10000ll&&d<=6227020800ll&&!f)
cout<<d<<endl;
[/cpp]

The ll indicates long long.

shamim
A great helper

Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Dhaka

Fixed how shamin said and you have to handle negative number with the funtion gama you have to see if is odd or even for each case you have 2 diferents anwers -1 overflow and -2 underflow
Hope it helps
Keep posting
P.S. Sorry for my english

Ghust_omega
Experienced poster

Posts: 115
Joined: Tue Apr 06, 2004 7:04 pm
Location: Venezuela

### 10323 compile error

why it gives me compile error
#include <stdio.h>

void main()
{
long double fact[6],f=5040,i,j;
long num;

for(i=8;i<=13;i++)
{
f=f*i;
fact[i-8]=f;
}

while(scanf("%ld",&num)==1)
{
if(num<8)
printf("Underflow!\n");
else if(num>13)
printf("Overflow!\n");

else printf("%.0Lf\n",fact[num-8]);
}
}
Gazi Shaheen Hossain
New poster

Posts: 7
Joined: Fri Sep 03, 2004 6:47 pm

PreviousNext