Idea for Recursive Algorithm

Oct 2019
9
0
UAE
I have a question and I knew you cannot give a complete solution and I need some tips.

I should solve this problem with the recursion algorithm.

For the last row: The first and second numbers add together then second and third numbers add together and finally third and fourth number add together. The same way for the third and second row.

Are you have some ideas? Thanks in advance
 

Attachments

skipjack

Forum Staff
Dec 2006
21,479
2,470
Start by calculating (recursively), say,
2 4 6 8
6 10 14
16 24
40
then (recursively) output the rows in reverse order.
 
  • Like
Reactions: 2 people
Oct 2019
9
0
UAE
Start by calculating (recursively), say,
2 4 6 8
6 10 14
16 24
40
then (recursively) output the rows in reverse order.
Thank you for your answer,
I couldn’t find any recursively formula.
Could you please give me an similar formula?
 
Dec 2015
1,084
169
Earth
Mark each sequence with a label :LABEL then call them in reverse.
 
May 2013
100
10
Here's a fairly simple way.
in python;
Code:
startnum = input() """write the bottom row of numbers separated by one space"""
startnum = startnum.split() """this will store the numbers in an array"""
for i in  range(0,len(startnum)):
    startnum[i] = int(startnum[i])  """this will convert the numbers to integer rather than string"""
def add(startnum):
    if len(startnum) == 1:  """base case"""
      print(startnum)
      return
   else:
      for i in range(0,len(startnum)-1):    """general case""
         startnum[i] = startnum[i]+startnum[i+1]
      startnum.pop(-1)     """removes the last number from the list."""
      print(startnum)
      return add(startnum)
I'll leave you the task of printing from top to bottom.
 
Last edited by a moderator:
  • Like
Reactions: 1 person
Oct 2019
9
0
UAE
Here's a fairly simple way.
in python;
Code:
startnum = input() """write the bottom row of numbers separated by one space"""
startnum = startnum.split() """this will store the numbers in an array"""
for i in  range(0,len(startnum)):
    startnum[i] = int(startnum[i])  """this will convert the numbers to integer rather than string"""
def add(startnum):
    if len(startnum) == 1:  """base case"""
      print(startnum)
      return
   else:
      for i in range(0,len(startnum)-1):    """general case""
         startnum[i] = startnum[i]+startnum[i+1]
      startnum.pop(-1)     """removes the last number from the list."""
      print(startnum)
      return add(startnum)
I'll leave you the task of printing from top to bottom.
Thanks,
But I think it isn’t recursive algorithm. You use iteration (for) instead of recursive.
In recursive algorithms we don’t use for loop in our algorithm.
 
Last edited:

skipjack

Forum Staff
Dec 2006
21,479
2,470
Any "for" loop can be implemented by use of recursion. That's a relatively minor coding issue.

Are you even allowed to use "split", given that you don't know whether it's implemented using recursion?
 
  • Like
Reactions: 1 person
Oct 2019
9
0
UAE
Any "for" loop can be implemented by use of recursion. That's a relatively minor coding issue.

Are you even allowed to use "split", given that you don't know whether it's implemented using recursion?
Thank you,
Yes, We can.

The above algorithm is semi-recursive. We don’t use iteration.
 
May 2013
100
10
i use a for loop to calc a row, and recurse to do the same on the next row.
so it's semi recursive.
if you absolutely need 100% recursive...

Code:
startnum = input() """write the bottom row of numbers separated by one space"""
startnum = startnum.split() """this will store the numbers in an array"""
for i in  range(0,len(startnum)):
    startnum[i] = int(startnum[i])  """this will convert the numbers to integer rather than string"""
def add(startnum):
    if len(startnum) == 1:  """base case"""
      print(startnum,0)
      return
   else:
     rowcalc(startnum)
      startnum.pop(-1)     """removes the last number from the list."""
      print(startnum)
      return add(startnum)

def rowcalc(startnum,i):
    if i == len(startnum):
       return
   else:
      startnum[i] = startnum[i] +startnum[i+1]
      i += 1
     return rowcalc(startnum,i)
 
Last edited:
  • Like
Reactions: 1 person