Jump to content

Non Negatives Factorials

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
I'm working on this problem where you have a variable n and you want to determine if n! is in a certain range. That is easy but the input data is giving a value for n that is less than zero.

Is there anything that I don't know that says what to do when n < 0?

I learned that factorial was defined recursively as:

f(n) = 1, 0 >= n >= 1
n*f(n-1), n > 1

However, when I implement this I get a wrong answer.

The limit is 10000 - 6227020800. So then n can only be in the range [8,13]. Is there anything I am missing?

Say I get f(-1)? Then what?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Short answer: (-1)! is not defined.
Long answer: look at Math Forum - Ask Dr. Math and Math Forum: Ask Dr. Math FAQ: 0! = 1

Most likely, you're running into an overflow condition.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Thanks. I got it. This problem is wrong then.

When you are given a negative integer that is even, you have to output Underflow, and if it is odd, output Overflow. This is a bad problem. :(

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Is it on the Online Judge?
Wow I changed my sig!

#5
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Yes.

The problem title is "Factorial - You Must Be Kidding me!!!!"

Just realized my title is misleading. :P