Factorial misbehavior - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: Factorial misbehavior (/thread-252028.html) Factorial misbehavior - Han - 10-02-2013 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 Re: Insufficient memory - Clayton Workman - 10-02-2013 Maybe similar reason why you can't do more than 1000 elements in a summation series? Hopefully, they can change this behaviour. -Clayton Re: Insufficient memory - Han - 10-02-2013 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. Re: Insufficient memory - Mark Hardman - 10-02-2013 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 Re: Insufficient memory - Han - 10-02-2013 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. Re: Insufficient memory - Mark Hardman - 10-02-2013 I'm unable to reproduce the Insufficient Memory bug as you described. Mark Hardman Re: Insufficient memory - Han - 10-02-2013 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. Re: Factorial misbehavior - Gilles Carpentier - 10-03-2013 Curious... I noted that there is no such limitation with the emulator Edited: 3 Oct 2013, 2:13 p.m. Re: Factorial misbehavior - Mark Hardman - 10-03-2013 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. Re: Factorial misbehavior - Ed Hodapp - 10-08-2013 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 LIST(Ans), I get the exact value of 255!. If I change the RETURN statement to RETURN LIST(f), I get Inf (which is annoying in itself - how many representations of infinity does HP require?). Re: Factorial misbehavior - Ed Hodapp - 10-09-2013 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.