简体   繁体   中英

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

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 = 48

I did did some part of the code but it gives wrong result and i dont know where im making mistakes. Thats 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))

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 N th 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

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))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM