Diego
Diego

Reputation: 77

Sum of N numbers in Fibonacci

I am trying to implement the total sum of N whole numbers in Fibonacci

def fibo(n):
    if n<2:
        return 1
    else:
        res = fibo(n-1) + fibo(n-2)
        sum = sum + res
        return res, sum

n=7
sum = 0
for i in range(1, n):
    print(fibo(i))

print("Suma", sum)

#example: if n=7 then print : 1,1,2,3,5,8,13 and sum is 32

The error I have is, when I put sum = sum + res Doesnt print & run the program

Currently, how could you implement the total sum?

Upvotes: 1

Views: 26688

Answers (8)

Alankrith G
Alankrith G

Reputation: 117

Considering the start of the Fibonacci series with 1 rather than 0.

def fib(no_of_elements):
    elements, start = [], 1
    while start <= no_of_elements:
        if start in [1, 2]:
            elements.append(1)
        elif start >= 3:
            elements.append(elements[start-2]+elements[start-3])
        start += 1
    return elements, sum(elements)

print(fib(8)) 

Output:
([1, 1, 2, 3, 5, 8, 13, 21], 54)

Upvotes: 0

Aravinth Ponnusamy
Aravinth Ponnusamy

Reputation: 11

def sumOfNFibonacciNumbers(n):

# Write your code here
i = 1
sum = 2
fib_list = [0, 1, 1]
if n == 1:
    return 0
if n == 2:
    return 1
if n == 3:
    return 2
for x in range(1,n-2):
    m = fib_list[-1] + fib_list[-2]
    fib_list.append(m)
    sum = sum + m
return sum

result = sumOfNFibonacciNumbers(10) print(result)

Upvotes: 1

Jonas Wolff
Jonas Wolff

Reputation: 2244

actually i don't think this needs to be that complicated the fibonacci sequence is very interesting in a maltitude of ways for example, if you want the sum up the 7th fibonacci number, then have checked what the 9th fibonacci number - 1 is? Now how do we find the n'th fibonacci number?

p = (1+5**.5)/2
q = (1-5**.5)/2
def fibo(n):
    return 1/5**.5*(p**n-q**n)

and now we can can find the sum up to any number in one calculation! for example for 7

fibo(9)- 1

output

33

and what is the actual answer

1+1+2+3+5+8+13=33

summa summarum: the fibonachi number that is two places further down the sequence minus 1 is the sum of the fibonachi numbers up to the number

Upvotes: 1

Samuel Nde
Samuel Nde

Reputation: 2743

Let me first point out that the sum of the first 7 terms of the Fibonacci sequence is not 32. That sum is 33. Now to the problem. Here is how I would solve the problem. I would first define the function that calculates the n th term of the Fibonacci sequence as follows:

def fibo(n):
    if n in [1,2]:
        return 1
    else:
        res = fibo(n-1) + fibo(n-2)
    return res

Then I would define a function to calculate the sum of the first n terms of the Fibonacci sequence as follows.

def sum_fibo(n):
    res = [fibo(i) for i in range(1, n+1)]
    print(res)
    return sum(res)

So if I do

[In] sum_fibo(7)

I get

        [1, 1, 2, 3, 5, 8, 13]
out >>> 33

NOTE: In defining the functions above, I have assumed that the input of the function is always going to be a positive integer though the Fibonacci can be extended to cover all real and complex numbers as shown on this wiki page.

Upvotes: 1

Franndy Abreu
Franndy Abreu

Reputation: 186

You are referring the variable sum before assignment.

You may want to use the variable sum inside the for loop and assign the fibo to it.

    def fibo(n):
        if n<2:
            return 1
         else:
            return fibo(n-1) + fibo(n-2)


n=7
sum = 0
for i in range(1, n):
    sum +=  fibo(i)
    print(fibo(i))

print("suma", sum)

Upvotes: 0

AGGELOS
AGGELOS

Reputation: 11

You simply need to calculate sum in the for loop, not in the fibo(n). Here take a look:

def fibo(n):
if n<2:
    return 1
else:
    res = fibo(n-1) + fibo(n-2)
    return res

n=7
sum = 0
for i in range(0, n):
    r = fibo(i)
    sum += r
    print(r)

print("Suma", sum)

I used r in order to call fibo once in each loop.

Upvotes: 1

rafaelc
rafaelc

Reputation: 59274

First of all, the line sum = sum + res makes no sense because you never defined sum in the first place.

So, your function should look like

def fibo(n):
    if n<2:
        return 1
    else:
        return fibo(n-1) + fibo(n-2)

Second, you can get the sum by simply

sum_ = 0
for i in range(0, n):
    sum_ += fibo(i)

Or maybe

sum_ = sum(fibo(i) for i in range(0, n))

Notice that the latter would only work if you have not overridden the built-in function named sum

Upvotes: 0

SmitM
SmitM

Reputation: 1376

Made some modifications to your code:

def fibo(n):
    print(1)
    counter = 1
    old_num = 0
    new_num = 1
    sum_fib = 1
    while counter < n:
        fib = old_num + new_num
        print(fib)

        if counter < n:
            old_num = new_num
            new_num = fib
            sum_fib = sum_fib + fib
            counter = counter + 1

    print('sum:' + str(sum_fib))


#fibo(5)

Upvotes: 0

Related Questions