Sum of N numbers in Fibonacci
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)
Diego
Updated on July 06, 2022Comments
-
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 programCurrently, how could you implement the total sum?
-
cs95 over 5 years....So....? It looks like it works. What is your problem? Just add
fibo(i)
tosuma
at each iteration... what else? -
wim over 5 yearsYou forgot the most important part of asking a question, which is asking the question.
-
Diego over 5 yearsSry, i edit now ...
-
rafaelc over 5 yearsdon't name your variable
sum
-
Diego over 5 yearsHow do I implement the total sum? @RafaelC
-
Diego over 5 yearsJust add the total sum @coldspeed
-
rafaelc over 5 yearsinstead of
print(fibo(i))
dosum_ += fibo(i)
(and rename your variable fromsum
to something that doesn't override the built-in functionsum
, such assum_
) -
Diego over 5 yearsResult: invalid syntax in ` sum_+=fibo(i) ` @RafaelC
-
-
rafaelc over 5 yearsAny reason why you changed from a recursive function to a loop?
-
SmitM over 5 yearsNot really. Didn't say it had to be recursive
-
Roshin Raphel over 3 yearsPlease provide an explanation to your code, so the OP can understand it better.
-
Saif Asif over 3 yearsAlso please edit your answer with correct formatting
-
Tadija Bagarić almost 3 yearsCool 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