473,379 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How to organize a function that computes the fibonacci sequence

I am trying to create a function that prints the first 4 numbers in the fibonacci sequence, the first 10 numbers in the fibonacci sequence, and then "the first -4" numbers in the fibonacci sequence. The "-4" numbers in the fibonacci sequence should return an empty list because there is no "-4" numbers in the sequence. I need the function to print the 3 lists as an end result in the main() function. Here is my code so far, I'm new to functions so any help would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. def F(n):
  2.     if n == 0: return 0
  3.     elif n ==1: return 1
  4.     else: return F(n-1)+F(n-2)
  5. main():
  6.     print F(4)
  7.     print F(10)
  8.     print F(-4)
  9.  
  10.  
Feb 17 '15 #1
3 1220
bvdet
2,851 Expert Mod 2GB
First, you have to modify your function F() to accommodate negative integers. According to Wikipedia, the calculation can be generalized with this formula:
Expand|Select|Wrap|Line Numbers
  1. F[-n] = (-1)**(n+1)*F[n]
So, you would calculate the number for F[n] and apply it in the above equation for the negative index.

Print a range of numbers using the built-in range function.
Expand|Select|Wrap|Line Numbers
  1. >>> print ("[%s]" % (", ".join([str(F(n)) for n in range(-4,11)])))
  2. [-3, 2, -1, 1, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
  3. >>> 
Feb 17 '15 #2
I don't need it to find the negative numbers, I need the -4 to return as an empty list. the output should look like this

[0, 1, 1, 2]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
[]
Feb 17 '15 #3
bvdet
2,851 Expert Mod 2GB
According to the problem you presented, you need to show the first -4 numbers. This means to me one of the following:
Expand|Select|Wrap|Line Numbers
  1. >>> range(0, -4, -1)
  2. [0, -1, -2, -3]
  3. >>> range(-1, -5, -1)
  4. [-1, -2, -3, -4]
  5. >>> 
If my understanding is wrong, you have your answer.
Feb 17 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: misho | last post by:
Write a C program using the fork() system call that that generates the Fibonacci sequence in the child process. The number of the sequence will be provided in the command line. For example, if 5 is...
5
by: coleslaw01 | last post by:
Hello, I am trying to teach myself C++ while babysitting a stable network in Iraq and have put together a program to display the fibonacci sequence. It works with long and long double(output in...
16
by: showellshowell | last post by:
I would like to present an unusual take on computing the Fibonacci sequence using Javascript. Please try out the following link in Firefox (sorry, having problems with other browsers): ...
8
by: sedaw | last post by:
need to print the n fibonacci number . this is my work dont know why it isnt workin . #include <stdio.h> void main() { int n, F0=0, F1=1, F, i=0; printf("N=?/n"); scanf("%d", &n);
1
by: altaey | last post by:
Question Details: Write a program to find and print a Fibonacci sequence of numbers. The Fibonacci sequence is defined as follow: Fn = Fn-2 + Fn-1, n >= 0 F0 = 0, F1 = 1, F2 = 1 Your...
3
by: nikid72 | last post by:
I am having a problem with a program I have written, the requirements were to "Write a function that computes and returns the n-th Fibonacci number. The main should invoke that function and print...
2
by: tirantha | last post by:
I wanna wright C programe to find value F(n)value with in(0=<n<=20),programme must be we input n valu then we get out put F(n) value. fibonacci sequence F(n)=F(n-1)+F(n-2) #include<stdio.h> int...
1
by: hina rehman | last post by:
Q) How to do nested loops? Q) how to do Array? Q hot to do Fibonacci sequence?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.