Sum of N numbers in Fibonacci

20,225

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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)

Share:
20,225
Diego
Author by

Diego

Updated on July 06, 2022

Comments

  • Diego
    Diego almost 2 years

    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?

    • cs95
      cs95 over 5 years
      ....So....? It looks like it works. What is your problem? Just add fibo(i) to suma at each iteration... what else?
    • wim
      wim over 5 years
      You forgot the most important part of asking a question, which is asking the question.
    • Diego
      Diego over 5 years
      Sry, i edit now ...
    • rafaelc
      rafaelc over 5 years
      don't name your variable sum
    • Diego
      Diego over 5 years
      How do I implement the total sum? @RafaelC
    • Diego
      Diego over 5 years
      Just add the total sum @coldspeed
    • rafaelc
      rafaelc over 5 years
      instead of print(fibo(i)) do sum_ += fibo(i) (and rename your variable from sum to something that doesn't override the built-in function sum, such as sum_)
    • Diego
      Diego over 5 years
      Result: invalid syntax in ` sum_+=fibo(i) ` @RafaelC
  • rafaelc
    rafaelc over 5 years
    Any reason why you changed from a recursive function to a loop?
  • SmitM
    SmitM over 5 years
    Not really. Didn't say it had to be recursive
  • Roshin Raphel
    Roshin Raphel over 3 years
    Please provide an explanation to your code, so the OP can understand it better.
  • Saif Asif
    Saif Asif over 3 years
    Also please edit your answer with correct formatting
  • Tadija Bagarić
    Tadija Bagarić almost 3 years
    Cool answer, but this has precision problems. For n=7 it would give 33.007272246494836 instead of 33. If rounded to nearest integer, it will start giving wrong results already at n <= 100