Article:
http://www.chacha.com/topic/solar-pa...r-panel-design
Code:
Code:
# Humza Tariq
# Computer Science I
# McMaster University
# Program Description: This program is capable of doing two things: 1) Compute the Fibonacci terms till the nth number you desire
# or it can compute the sum of all the Fibonacci terms you desire, or do both!
def computeAllFib(n):
list=[0,1,1]
initial = 3
x = 1
y = 1
for i in range(initial,n+1):
x,y=x+y,x
list.append(x)
return list[0:n+1]
def findSum(n):
x = computeAllFib(n)
sum = 0
for i in x:
sum = sum + i
return sum
def main():
choice = int(input("Enter '1' to compute all Fibonacci terms till the nth number,\nor '2' to compute the sum of all Fibonacci numbers till the nth one or anything else for both: "))
desiredNumber = int(input("What is the nth number?"))
if choice == 1:
c1 = computeAllFib(desiredNumber)
print c1
elif choice == 2:
c2 = findSum(desiredNumber)
print c2
else:
c1 = computeAllFib(desiredNumber)
c2 = findSum(desiredNumber)
print "The numbers are: "+str(c1)+".","The sum is "+str(c2)+"."
main()
I made this little program using Python. Is there anything I can to do it to make it more efficient and not change the code substantially? If that isn't possible, what edits should I make to the code so it's still understandable to me and (much more) efficient. I also noticed..there isn't a code snippets sub-section anywhere on this forum which would be cool idea. Thanks in advance,
Humza