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. - def F(n):
-
if n == 0: return 0
-
elif n ==1: return 1
-
else: return F(n-1)+F(n-2)
-
main():
-
print F(4)
-
print F(10)
-
print F(-4)
-
-
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:
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. - >>> print ("[%s]" % (", ".join([str(F(n)) for n in range(-4,11)])))
-
[-3, 2, -1, 1, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
-
>>>
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]
[]
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: - >>> range(0, -4, -1)
-
[0, -1, -2, -3]
-
>>> range(-1, -5, -1)
-
[-1, -2, -3, -4]
-
>>>
If my understanding is wrong, you have your answer.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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):
...
|
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);
|
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...
|
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...
|
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...
|
by: hina rehman |
last post by:
Q) How to do nested loops?
Q) how to do Array?
Q hot to do Fibonacci sequence?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |