Page 3 of 8

Re: Give some explanation

Posted: Sun Aug 17, 2003 11:23 am
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:

Posted: Sun Aug 17, 2003 11:26 am
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

Posted: Mon Aug 18, 2003 9:53 am
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.

Posted: Mon Aug 18, 2003 4:46 pm
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.

Posted: Sun Oct 12, 2003 7:12 pm
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]

10323 help me

Posted: Mon Nov 10, 2003 2:31 am
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();
}
}

Posted: Mon Nov 10, 2003 5:27 am
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:

Re: 10323 help me

Posted: Wed Nov 19, 2003 8:02 am
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

Posted: Sat Jan 10, 2004 4:57 pm
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

Thanx

Posted: Wed Jan 14, 2004 6:24 pm
by aakash_mandhar
Thx Bug!
I was really frustrated coz factorial of negative number :o .. Thx


Aakash

10323 compiler error!

Posted: Sun Aug 15, 2004 10:45 am
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]

Posted: Sun Aug 15, 2004 12:13 pm
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.

Posted: Sun Aug 15, 2004 4:38 pm
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.

Posted: Wed Sep 01, 2004 11:03 pm
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

10323 compile error

Posted: Mon Sep 06, 2004 5:24 pm
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]);
}
}