Post Reply 
(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


Attached File(s)
.pdf  HP15C - Some digits for a sum - MoHPC - 1st April 2020.pdf (Size: 25.83 KB / Downloads: 10)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(HP15C / HP41 ) Recursive program of the 1st April - Nihotte(lma) - 04-01-2020 08:19 PM



User(s) browsing this thread: 1 Guest(s)