0

How can i calculate and print the sum of first N elements of below sequence using recursive function

Example: Sample Input N: 4

Sample Output: 7 + 12 + 17 + 22 = 58

I did some part of the code but it gives wrong result and I don't know where I'm making mistakes. That's why I need help!

def recur_sum(n):
  if n <= 1:
      return n
  else:
      for i in range(7,n+1):
          return i + recur_sum(i-1)
      

num = int(input("Enter a number: "))

if num < 0:
  print("Enter a positive number")
else:
  print("The sum is",recur_sum(num))
Mabel
  • 1
  • 1
  • 2
    How do choose the 4 numbers ? What is "below sequence" ? – azro Dec 11 '21 at 08:49
  • range from 7 to 5 has no values, so it ends up directly – azro Dec 11 '21 at 08:50
  • When you ask a question, you need to stay around fot the next 10-15 minutes, if not the question will be closed/no one will come to see it, as there is new questions every minute, your one will be lost in the sea ;) – azro Dec 11 '21 at 08:58
  • @BehdadAbdollahiMoghadam please don't try to know what the OP exactly want, his post is unclear, there is missing information. You can answer, but try to convince others that this is the way ;) – azro Dec 11 '21 at 09:11

3 Answers3

0

As far as I understand your question, you want the sum of a sequence which each element of this sequence is increased by a step (such as 5), and it has an initial value (like 7), and you want the sum of first N (like 4) elements of this sequence.

At each recursion level, we add the current value to step , but when n == 1 we only return the current value (which is the Nth item of the sequence).

Do this:

def recur_sum(cur_val, step, n):
    if n == 1:
        return cur_val
    return cur_val + recur_sum(cur_val+step,step,n-1)
      

num = int(input("Enter a number: "))
init_val = 7
step = 5

if num < 0:
  print("Enter a positive number")
else:
  print("The sum is",recur_sum(init_val, step, num))

Output:

The sum is 58
0

this returns a list of all sequential numbers starting at 7 each increment of 5. sum the return array.... change 5 and 2 to change increments for desired jumps/steps, and change the initial return value..

def recur_sum(n):
    if n == 1:
        return [7]
    else:
        return [5*n+2] + recur_sum(n-1) 

num = int(input("Enter a number: "))

res = recur_sum(num)
print(sum(res))
huzzzus
  • 172
  • 10
0

A recursive function which returns all the elements up to n (where n is the input of your function) has already been proposed above. In my understanding, you want a function with some recursive logic that return the sum of all the elements up to the n-th. Your sequence is 7, 12, 17, 22, 27 and so forth. If we disect it:

it   element      sum         sum is             element is
 1         7        7         1 * 7 +  0 * 5     1 * 7 + 0 * 5
 2        12       19         2 * 7 +  1 * 5     1 * 7 + 1 * 5
 3        17       36         3 * 7 +  3 * 5     1 * 7 + 2 * 5
 4        22       58         4 * 7 +  6 * 5     1 * 7 + 3 * 5
 5        27       85         5 * 7 + 10 * 5     1 * 7 + 4 * 5

If you want at any cost to implement a recursive solution, if is evident that at each step you need to increase the rolling sum by it * 7 + (it - 1) * 5 (where 7 is your start point, while 5 is your step). You can implement a recursive solution as follows:

def recursive(n, step = 5, start = 7, counter = 1):
    if n  >  0:
        this_element = start + (counter - 1) * step
        if counter == n:
            return this_element
        else:
            return this_element + recursive(n, step = step, start = start, counter = counter + 1)
    else:
        return 0    

for i in range(1, 10):
    print(recursive(i))

OUTPUT

7
19
36
58
85
117
154
196
243

From the table above you can see though that maybe a recursive solution is overkilling here given that the sum of elements up to n-th step has solution:

def my_sum(n, step = 5, start = 7):
    return n * start + int(step * (n - 1) * n / 2)

for i in range(1, 10):
    print(my_sum(i))

OUTPUT

7
19
36
58
85
117
154
196
243
nikeros
  • 3,302
  • 2
  • 10
  • 26