# HP Forums

Full Version: Factorial misbehavior
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Is there a built-in limit on the size of the result of factorial() ? It seems that the limit is 254! and 255! is just "infinity." I was hoping it would be limited only by available memory (32MB RAM and 256MB Flash)....

...and perhaps got a little more than what I bargained for. I typed in 250! through 254! and then converted the result for 254! into a string (length is 503 chars). With those results on the CAS screen, I proceed to create a new program and lo and behold:

Insufficient Memory!

Say what?! I check my memory with [Shift][Mem] and see that I have only used up 4KB in the CAS.

Edited: 2 Oct 2013, 10:18 p.m. after one or more responses were posted

Maybe similar reason why you can't do more than 1000 elements in a summation series? Hopefully, they can change this behaviour.

-Clayton

Quote:
Maybe similar reason why you can't do more than 1000 elements in a summation series? Hopefully, they can change this behaviour.

-Clayton

Were you able to confirm with your own calculator? I checked on the emulator and I can do up to 946! If I then copy the number (in CAS mode) and do + "" (cheap conversion to string) then the emulator crashes. 947! returns infinity. On the actual HP Prime, 255! returns infinity whereas 254! is ok.

Edited: 2 Oct 2013, 9:26 p.m.

It would seem that the upper limit on factorial is artificial.

In CAS mode, 254!*255*256*257*258 returns a valid, non-infinite value.

In fact, 254!*254!*254!*254! returns an integer with 2,009 digits.

Mark Hardman

Quote:
It would seem that the upper limit on factorial is artificial.

In CAS mode, 254!*255*256*257*258 returns a valid, non-infinite value.

In fact, 254!*254!*254!*254! returns an integer with 2,009 digits.

Mark Hardman

Thanks for verifying. If you are daring enough to try to take one of those results and add an empty string to it, and then try to create a new program, that would be greatly appreciated too. (If you run into an insufficient memory error, a soft reset via [ON][APPS] should fix it).

I created my own factorial program in CAS mode and was able to get up to 946! without issues. However, 947! again returns infinity. 946! is 2407 digits -- not sure if there is any significance there.

Edit: cannot seem to duplicate the insufficient memory error any more, though.

Edited: 2 Oct 2013, 10:07 p.m.

I'm unable to reproduce the Insufficient Memory bug as you described.

Mark Hardman

Thanks for the response and testing. I think I may have found the cause of the insufficient memory. If you do happen to create a really large integer (say 946!), the screen stutters when redrawing. E.G. press [Apps] and then go back to [CAS] with the result for 946! on the history.

Edit: the ins. mem. error is not related to factorials, but had to do with an invalid matrix that managed to stay resident in memory.

Edited: 3 Oct 2013, 1:06 p.m.

Curious...

I noted that there is no such limitation with the emulator

Edited: 3 Oct 2013, 2:13 p.m.

A limitation still exists in the emulator. There, the maximum allowed value is is 946! This is the same limit Han reached using his own CAS based factorial program on the actual hardware.

Mark Hardman

Edited: 3 Oct 2013, 3:07 p.m.

The reason is that somewhere, something is converting to floating point instead of multi-precision integers. It appears that HP was a little careless in dealing with object types this time around. Even in CAS mode, various things will get converted. Here's a little test program:

```EXPORT IFACT(n)
BEGIN
LOCAL i,f;
{1}->f;
IF n>1 THEN
FOR i FROM 2 TO n DO
CONCAT(f,i)->f;
END;
RETURN f;
END;
```

If I run IFACT(255) in CAS mode, and then do <Pi>LIST(Ans), I get the exact value of 255!. If I change the RETURN statement to RETURN <Pi>LIST(f), I get Inf (which is annoying in itself - how many representations of infinity does HP require?).

Oops, that was wrong - using a float, 253! is the largest that can be represented. Looks like someone arbitrarily decided that 255! is just too big. "Why would anyone need more than 640k?" they must have Bill Gates helping them.