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

Moderator: Board moderators

sms.islam
New poster
Posts: 19
Joined: Sat Oct 10, 2009 10:28 am

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

code removed.After accepted!!!
Last edited by sms.islam on Sun Oct 18, 2009 12:04 pm, edited 1 time in total.

sms.islam
New poster
Posts: 19
Joined: Sat Oct 10, 2009 10:28 am

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

i got it!!! just precalculate the factorial.....oops its easy!!!

refat_ruet
New poster
Posts: 3
Joined: Fri Aug 20, 2010 10:24 am

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

can anyone help me...this code keep saying wrong answer..
#include<stdio.h>
long long int fact(long long int x);
long long por(long long int z);
int main(){
long long int n,z,y;
while(scanf("%llu",&n)==1){
z=fact(n);
y=por(z);
}

return 0;}
long long int fact(long long int x){
int i,s=1;
for(i=2;i<=x;i++)
s=s*i;
}
long long por(long long int z){
if(z>6227020800)
printf("Overflow!");
else if(z<10000)
printf("Underflow!") ;
else
printf("%llu",z);}

Learning poster
Posts: 74
Joined: Fri May 08, 2009 5:16 pm

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

plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

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

Lol this problem is indeed mathematically wrong. The factorial of negative integers are undefined according to the gamma function. The definition of the factorial function is:
(An example of a piece-wise defined function)
0! = 1
n! = n*(n-1)! for n > 0
I found in some previous posts that people said the factorial of 0 is 0....so the factorial of - 1 should be 0!/ 0. I am just writing this post for clarification.

First of all, note that 0! is not equal to 0. 0! = 1 (Check it out with your calculator ). The reason for this is demonstrated as below:
5! = 120
4! = 5! / 5
3! = 4! / 4
2! = 3! / 3
1! = 2! / 2
So, 0! = 1! / 1

An example of backtracking.

Another example:
nCk is the number of combinations possible you can choose k objects from a given set set of n objects.
By definition:
nCk = n! / (k! * (n - k)!)
So 0! = 1 neatly fits what we expect nC0 and nCn to be.

Also note that, before I give you the judge's logic(which is of course incorrect) for this problem, you must first understand the true logic. Anything divided by 0, Say 4 / 0 is NOT inifinity, rather it is not defined. That is why mathematicians refer to numbers that are divided by 0 as "undefined". (There is a special term for this called "indeterminate", search the web for more info) Some people tend to think of them as being infinite, but this isn't exactly true. There simply is no answer.

So We know that 0! = 1 now. We can use the same backtracking method to find -1!. Note again, that it is not possible to find factorials of negative numbers. By definition, -1! = 0! / 0
So, -1! = 1 / 0 = indeterminate
Again, -2! = -1! / -1 = indeterminate too(As -1! doesn't exist on the numerator, it follows that -2! also doesn't exist).

However the judge says that:
0! = 1 (True so this is an underflow)
-1! = 1 / 0 = +infinity = overflow (Wrong logic)
-2! = -1! / -1 = +infinity / -1 = -infinity = underflow(Again wrong logic!)
-3! = -2! / -2 = -infinity / -2 = + infinity = overflow......

I hope you understand now, why you need to check if n <= 0 then if (n % 2 == 0) = Underflow! and if (n % 2 != 0) = Overflow!
Note that this is mathematically and logically incorrect. This problem is just simply wrong(Wrong just because of the input contains negative numbers, if the dataset consisted of only positive numbers then the problem would be just fine)....its solution is wrong too. I just demonstrated the false logic which the judge uses.
Best Regards
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson

anjanpstu
New poster
Posts: 7
Joined: Thu Apr 07, 2011 8:40 am

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

got accepted This is the most horror problem that I have ever solved.thank you online-judge.
but the code is simple after understanding

simply code the problem
only for n=8to n=13 value will be printed otherwise not
no need of thinking difficulty

plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

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

Actually, the most creepiest problem I have tried here is Problem no 139. This factorial problem is just illogical, that's all.
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson

shoaib7k
New poster
Posts: 10
Joined: Thu Aug 18, 2011 7:45 pm

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

remove code after accepted.....
Last edited by shoaib7k on Mon Aug 29, 2011 6:06 pm, edited 1 time in total.

plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

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

Check these lines:

if(n%2==0)
printf("Underflow!\n");
else
printf("Overflow\n");
}

You forgot to include a factorial for Overflow.
It should be Overflow!
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson

shoaib7k
New poster
Posts: 10
Joined: Thu Aug 18, 2011 7:45 pm

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

thankssss/......
got accepted......

Sultan_Dhaka
New poster
Posts: 2
Joined: Wed Nov 02, 2011 8:26 am

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

I can not understand what is the error in this code ?? Why is it not accepted ?? Hi guyz ! Please help me

#include<iostream>
using namespace std;

long int array[15];

void factorial(int N)
{
int i,result=1;
for( i=2;i<=N;i++)
result=result*i;
array[N]=result;
}

int main()
{
long int N;
for(int i=7;i<=14;i++)
factorial(i);
while(cin>>N)
{
if(N<0)
{
N=N*(-1);
if(N%2==1)
cout<<"Overflow!"<<endl;
else
cout<<"Underflow!"<<endl;
}
else if(N<8)
cout<<"Underflow!"<<endl;
else if(N>13)
cout<<"Overflow!"<<endl;
else
cout<<array[N]<<endl;

}
return 0;
}

what!
New poster
Posts: 2
Joined: Thu Jun 07, 2012 7:53 pm

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

#include <stdio.h>

int main()
{
int n,i;
long long int sum;
while((scanf("%d",&n)==1))
{

sum=1;
if(n<8&&n>0)
printf("Underdflow!\n");
else if(n>13)
printf("Overflow!\n");

else if(n>=8&&n<=13)
{
for(i=n;i>=1;i--)
{
sum=sum*i;
}
printf("%lld\n",sum);
}
else if(n<=0)
{
if(n%2==0)
printf("Underflow!\n");
else
printf("Overflow!\n");
}
}

return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

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

You misspelled Underflow
Check input and AC output for thousands of problems on uDebug!

what!
New poster
Posts: 2
Joined: Thu Jun 07, 2012 7:53 pm

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

brianfry713 wrote:You misspelled Underflow
Thanks. It is now AC.

akidsultan
New poster
Posts: 1
Joined: Sat Dec 01, 2012 9:12 pm

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

i got wrong answer but i don't find my fault .......................
pls help me..............
here is my code.....

#include<stdio.h>
long long int fact(long long int n)
{
if(n==0)
return 1;
else
return (n*fact(n-1));
}
int main()
{
long long int a;
while(scanf("%lld",&a)!=EOF)
{
if(a<0&&a%2==1)
printf("Underflow!\n");
else if(a<0&&a%2==0)
printf("Overflow!\n");
else if(a==0||a<=7)
printf("Underflow!\n");
else if(a>13)
printf("Overflow!\n");
else
{
long long int i=fact(a);
printf("%lld\n",i);
}
}
return 0;
}