Factorial misbehavior Han Unregistered Posts: 709 Threads: 104 Joined: Nov 2005 10-02-2013, 08:16 PM 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 Clayton Workman Unregistered Posts: 15 Threads: 1 Joined: Sep 2013 10-02-2013, 08:54 PM Maybe similar reason why you can't do more than 1000 elements in a summation series? Hopefully, they can change this behaviour. -Clayton Han Unregistered Posts: 709 Threads: 104 Joined: Nov 2005 10-02-2013, 09:21 PM 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. Mark Hardman Unregistered Posts: 208 Threads: 16 Joined: Jul 2005 10-02-2013, 09:46 PM 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 Han Unregistered Posts: 709 Threads: 104 Joined: Nov 2005 10-02-2013, 10:05 PM 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. Mark Hardman Unregistered Posts: 208 Threads: 16 Joined: Jul 2005 10-02-2013, 10:15 PM I'm unable to reproduce the Insufficient Memory bug as you described. Mark Hardman Han Unregistered Posts: 709 Threads: 104 Joined: Nov 2005 10-02-2013, 10:18 PM 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. Gilles Carpentier Unregistered Posts: 468 Threads: 17 Joined: May 2011 10-03-2013, 02:11 PM Curious... I noted that there is no such limitation with the emulator Edited: 3 Oct 2013, 2:13 p.m. Mark Hardman Unregistered Posts: 208 Threads: 16 Joined: Jul 2005 10-03-2013, 02:52 PM 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. Ed Hodapp Unregistered Posts: 2 Threads: 0 Joined: Oct 2013 10-08-2013, 09:55 AM 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?). Ed Hodapp Unregistered Posts: 2 Threads: 0 Joined: Oct 2013 10-09-2013, 02:36 AM 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. « Next Oldest | Next Newest »

 Possibly Related Threads… Thread Author Replies Views Last Post HP41C: Factorial (kind of) in MCODE Frido Bohn 7 2,349 05-26-2012, 09:18 AM Last Post: Frido Bohn Gamma (and Factorial) for 12C/12CP Les Wright 9 2,682 02-02-2009, 06:18 PM Last Post: Egan Ford Gamma and Factorial for 10C/12C/12CP (thread hijack) Les Wright 1 991 01-28-2009, 08:19 PM Last Post: Egan Ford hp33s and factorial timing Marcel Pelletier 5 1,425 07-30-2004, 05:06 AM Last Post: Steve A

Forum Jump: