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

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

Re: Give some explanation

Post by InOutMoTo »

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

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 :wink:
DD
Experienced poster
Posts: 145
Joined: Thu Aug 14, 2003 8:42 am
Location: Mountain View, California
Contact:

Post by DD »

I think you don't consider one situation

that is if the input number is negative :o

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. :D
Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan »

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.
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

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.
romankrejci
New poster
Posts: 1
Joined: Sun Oct 12, 2003 6:53 pm
Location: Czech republic
Contact:

Post by romankrejci »

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]
problem
New poster
Posts: 27
Joined: Mon Nov 10, 2003 12:40 am

10323 help me

Post by problem »

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-48)*k;
t=(fmod(sum,10))+48;
i++;
c=sum/10;
}
while(c>0)
{
t[i++]=(c%10)+48;
c=c/10;
}
t='\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-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();
}
}
Joseph Kurniawan
Experienced poster
Posts: 136
Joined: Tue Apr 01, 2003 6:59 am
Location: Jakarta, Indonesia

Post by Joseph Kurniawan »

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!!! :wink: :wink: :wink:
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.
Bug!
New poster
Posts: 24
Joined: Thu Oct 30, 2003 10:19 am

Re: 10323 help me

Post by Bug! »

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
kiha
New poster
Posts: 37
Joined: Sat Dec 20, 2003 10:59 pm

Post by kiha »

Thank you,

I wasted about 5 submissions on this problem before reading this.
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
aakash_mandhar
New poster
Posts: 38
Joined: Thu Dec 11, 2003 3:40 pm
Location: Bangalore

Thanx

Post by aakash_mandhar »

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


Aakash
...I was born to code...
oulongbin
Learning poster
Posts: 53
Joined: Sat Jul 10, 2004 5:57 pm
Location: Shanghai China

10323 compiler error!

Post by oulongbin »

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]
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel »

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.
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim »

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

I made the correction and your code now gets WA.
Ghust_omega
Experienced poster
Posts: 115
Joined: Tue Apr 06, 2004 7:04 pm
Location: Venezuela

Post by Ghust_omega »

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
Gazi Shaheen Hossain
New poster
Posts: 7
Joined: Fri Sep 03, 2004 6:47 pm

10323 compile error

Post by Gazi Shaheen Hossain »

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]);
}
}
Post Reply

Return to “Volume 103 (10300-10399)”