473,465 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to organize a function that computes the fibonacci sequence

6 New Member
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 1221
bvdet
2,851 Recognized Expert Moderator Specialist
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
depasqualen
6 New Member
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 Recognized Expert Moderator Specialist
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.