# Idea for Recursive Algorithm

#### Shervan360

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

• 7.8 KB Views: 8
• 14.8 KB Views: 5

#### skipjack

Forum Staff
Start by calculating (recursively), say,
2 4 6 8
6 10 14
16 24
40
then (recursively) output the rows in reverse order.

• 2 people

#### Shervan360

Start by calculating (recursively), say,
2 4 6 8
6 10 14
16 24
40
then (recursively) output the rows in reverse order.
I couldnâ€™t find any recursively formula.
Could you please give me an similar formula?

#### idontknow

Mark each sequence with a label :LABEL then call them in reverse.

#### phillip1882

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"""
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:
• 1 person

#### Shervan360

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"""
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
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?

• 1 person

#### Shervan360

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.

#### phillip1882

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"""
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 rowcalc(startnum,i)
• 