How can this program be organized recursively?

 

v20240901