Question:
Python - calculate a cumulative sum of the first 100 fibonacci numbers?
Kesh
2013-02-08 13:01:46 UTC
My answer for the question in the title is as follows. It is part my own and part looking around. I am trying to understand what it is happening in the following workout. Especially at line7 - for x in range(3, fibo_Number + 1). Why is it (3, fibo_Number +1) and not (1, fibo_Number+1). If you could kindly explain the following workout, would be grateful or point out if I am doing it wrong. Thank you.

Answer:

fibo_Number = 100

a, b = 0, 1

fibo_Sum = a + b

for x in range(3, fibo_Number + 1):
a, b = b, a + b
fibo_Sum += b

print "Fibonacci cumulative sum for first 100 is: ", fibo_Sum
Three answers:
Ari
2013-02-08 13:12:02 UTC
you're better off finding the first 100 fibonacci numbers and storing them in a list, then summing the list



def fib_100():

#declare fib_list to start as the first 2 fibonacci numbers 0 and 1

.....fib_list = [0,1]

.....count=2 #the first 2 numbers are already there

.....while count<100:

.........#store the next fibonacci number, which is the sum of the last 2, at the end of the list

..........newNumber = fib_list[count-1] + fib_list[count-2]

..........fib_list.append(newNumber)

..........#add 1 to count

...........count+=1

......#now, fib_list has the first 100 numbers. to sum them for loop over the list

......sum=0

.......for x in fib_list:

............sum+=x

.......print "The sum of the first 100 fibonacci numbers is " + str(sum)
mexicano
2016-11-07 13:00:01 UTC
Fibonacci Series In Python
branting
2016-12-16 13:47:35 UTC
Will grant a evidence for n = 2qA with q even-valued. define the sum of the 1st n Fibonacci numbers by using S(n): S(n) = u(a million) + u(2) + ... + u(n-a million) + u(n), the place u(i) is the i'th Fibonacci variety. Then S(n) = (u(3)-u(2)) + (u(4)-u(3)) + ... + (u(n+a million)-u(n)) + (u(n+2)-u(n+a million)) ==> S(n) = u(n+2) - a million, --- (a million) because of the fact that u(2) = a million. next, word the identification: united statest) = united statesa million)*u(t) + u(s)*u(t+a million), --- (2) which holds for all s >= 2, t >= a million. (that's easily proved by using induction - e.g. over t for fastened s - yet is additionally "considered" by using noting: united statest) = u(a million)*united statest-2) + u(2)*united statest-a million) = u(2)*united statest-3) + (u(a million)+u(2))*united statest-2) = u(2)*united statest-3) + u(3)*united statest-2) = ... = united states2)*u(t+a million) + united statesa million)*u(t+2) = united statesa million)*u(t) + u(s)*u(t+a million).) Now evaluate integers of the variety n = 2qA, the place q and A are integers without regulations on q (e.g. evenness) for the 2nd. employing (a million): S(2qA) = u(2qA+2) - a million = u(qA-a million)*u(qA+2) + u(qA)*u(qA+3) - a million, by using (2) = u(qA-a million)*{u(qA-a million)*u(2) + u(qA)*u(3)} + u(qA)*u(qA+3) - a million, applying (2) back. ==> S(2qA) = u(qA)*{2*u(qA-a million) + u(qA+3)} + (u(qA-a million)^2 - a million). --- (3) basically have been given to paintings on (u(qA-a million)^2 - a million) and we are achieved: For this purpose, evaluate u(m-a million)^2: u(m-a million)^2 = ( u(m) - u(m-2) )*( u(m-2) + u(m-3) ) = u(m)*u(m-2) + u(m)*u(m-3) - u(m-2)^2 - u(m-2)*u(m-3) = u(m)*(u(m)-u(m-a million)) + (2*u(m-2)+u(m-3))*u(m-3) - u(m-2)^2 - u(m-2)*u(m-3) ==> u(m-a million)^2 - u(m)^2 + u(m)*u(m-a million) = u(m-3)^2 - u(m-2)^2 + u(m-2)*u(m-3) = ... = u(a million)^2 - u(2)^2 + u(2)*u(a million), if m is even = ... = u(2)^2 - u(3)^2 + u(2)*u(3), if m is atypical ==> u(m-a million)^2 = u(m)^2 - u(m)*u(m-a million) + (-a million)^m. --- (4) subsequently, by using (4), u(qA-a million)^2 = (-a million)^(qA) + u(qA)*(u(qA) - u(qA-a million)) Subst. this into (3) and cut back ensuing expression: S(2qA) = u(qA)*{u(qA+a million) + u(qA+3)} + ((-a million)^(qA) - a million). subsequently, if q is even (e.g. q = 10, like interior the question), u(qA) | S(2qA), if q is atypical and A is even, u(qA) | S(2qA), if q and A are atypical, then u(qA) | (S(2qA) + 2).


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...