(HP15C / HP41 ) Recursive program of the 1st April
|
04-01-2020, 08:19 PM
Post: #1
|
|||
|
|||
(HP15C / HP41 ) Recursive program of the 1st April
Some digits for a sum !
To celebrate the today April 1, I offer a small recursive program (lol) for the HP15C and the HP41 The purpose is to express a number into several distinct digits which, added together, match this number With all the possibility, please... Almost easy ! Yes ! But each digit (from 1 to 9) should be use only a time in the final sum ! So, the bigger number you can targeted is 45 (9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 45) Example : 13 by 2 digits consists in (7 + 6), (8 + 5) and (9 + 4) I don't remember how and when this idea came to me, but my programs for HP15C et HP41 date back to 2004... Take the precaution of reserving space for the memory registers, by selecting partition - HP15C : 32 f DIM (i) - HP41 : SIZE 034 An example of operation : Type 23 ENTER 2 and - HP15C : GSB A - HP41 : XEQ DCOMP Read back : 76 R/S 85 R/S 94 R/S 0 Why recursive ? That's because the program is based on this recursive function : Find_Sum(array, target, max, sum, used, ind) go = true while (ind >= 1) and go do if (used + 1 == max) then if (sum + ind == target) then array[ind] = busy; go = false display(array) else if (sum + ind > target) then ind = ind - 1 else go = false end if else if (used + 1 < max) then if (sum + ind < target) then array[ind] = busy Find_Sum(array, target, max, (sum + ind), (used + 1), (ind - 1)) array[ind] = free; ind = ind - 1 else ind = ind - 1 end if else go = false end if wend For the example : initial call with Find_Sum([0, 0, 0, 0, 0, 0, 0, 0, 0], 13, 2, 0, 0, 9) the function returns (7, 6) (8, 5) and (9, 4) Parameters array is array [1..9] of free or busy (0 or 1) is 000000000 to 111111111 target is integer from 1 to 45 max is index from 0 to 9 sum is integer from 0 to 45 used is index // digit from 0 to 9 : used = INT ( LOG ( array )) + 1 // Sum of contents of array[i] when i grows from 1 to 9 ind is index from 0 to 9 |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
(HP15C / HP41 ) Recursive program of the 1st April - Nihotte(lma) - 04-01-2020 08:19 PM
RE: (HP15C / HP41 ) Recursive program of the 1st April - Gamo - 04-02-2020, 09:26 AM
RE: (HP15C / HP41 ) Recursive program of the 1st April - Nihotte(lma) - 04-02-2020, 09:39 AM
RE: (HP15C / HP41 ) Recursive program too - Nihotte(lma) - 04-05-2020, 03:31 PM
RE: (HP15C / HP41 ) Recursive program of the 1st April - Nihotte(lma) - 04-06-2020, 04:43 AM
RE: (HP15C / HP41 ) Recursive program of the 1st April - Nihotte(lma) - 04-10-2020, 11:48 AM
RE: (HP15C / HP41 ) Recursive program of the 1st April - Nihotte(lma) - 04-12-2020, 12:27 PM
RE: (HP15C / HP41 ) Recursive program of the 1st April - Nihotte(lma) - 04-16-2020, 01:45 PM
RE: (HP15C / HP41 ) Recursive program of the 1st April - Nihotte(lma) - 04-22-2020, 07:38 PM
RE: (HP15C / HP41 ) Recursive program of the 1st April - Nihotte(lma) - 04-25-2020, 03:57 PM
|
User(s) browsing this thread: 1 Guest(s)