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

Posted:

**Sat Oct 17, 2009 11:18 am**code removed.After accepted!!!

The Online Judge board

https://uva.onlinejudge.org/board/

Page **7** of **8**

Posted: **Sat Oct 17, 2009 11:18 am**

code removed.After accepted!!!

Posted: **Sun Oct 18, 2009 12:02 pm**

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

Posted: **Mon Aug 30, 2010 9:11 pm**

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);}

#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);}

Posted: **Sat Sep 04, 2010 8:35 am**

Think about negative numbers and read previous posts.

Posted: **Sun Jul 03, 2011 9:02 am**

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

(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

Posted: **Mon Jul 25, 2011 9:41 pm**

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

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

Posted: **Wed Jul 27, 2011 4:47 pm**

Actually, the most creepiest problem I have tried here is Problem no 139. This factorial problem is just illogical, that's all.

Posted: **Sun Aug 21, 2011 12:57 am**

remove code after accepted.....

Posted: **Sun Aug 28, 2011 3:30 pm**

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!

if(n%2==0)

printf("Underflow!\n");

else

printf("Overflow\n");

}

You forgot to include a factorial for Overflow.

It should be Overflow!

Posted: **Mon Aug 29, 2011 6:04 pm**

thankssss/......

got accepted......

got accepted......

Posted: **Tue Nov 08, 2011 9:10 pm**

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;

}

#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;

}

Posted: **Thu Jun 07, 2012 8:34 pm**

what is the wrong? please help.....

#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;

}

#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;

}

Posted: **Fri Jun 08, 2012 1:03 am**

You misspelled Underflow

Posted: **Fri Jun 08, 2012 9:40 pm**

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

Posted: **Sat Dec 01, 2012 9:25 pm**

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;

}

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;

}