1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Katyanochek1 [597]
3 years ago
7

Problem 6. (Fibonacci Number) Write a program fibonacci.py that accepts n (int) as command-line argument, and writes to standard

output the nth number from the Fibonacci sequence (1, 1, 2, 3, 5, 8, 13, . . . ).
Computers and Technology
1 answer:
77julia77 [94]3 years ago
7 0

Answer:

  1. import sys
  2. def fibonacci(n):
  3.    if(n == 0 or n == 1):
  4.        return n  
  5.    else:
  6.        return fibonacci(n - 2) + fibonacci(n-1)
  7. num = int(sys.argv)  
  8. output = ""
  9. for i in range(1, num+1):
  10.    output += str(fibonacci(i)) + " "  
  11. print(output)

Explanation:

Firstly, we import sys module as we need to accepts n as command line argument (Line 1).

Next, create a function fibonacci takes take one input n (Line 3). If the n is zero or one, return the n itself (Line 4-5). Otherwise, the program shall run the recursive call to the fibonacci function itself by putting n-2 and n-1 as function argument, respectively (Line 6-7).

After that, use the sys.argv to get the command line argument and assign the value to num (Line 9).

Use a for loop to generate the output string by progressively calling the fibonacci function and join the return result to the output string (Line 12-13).

At last print the output string (Line 15).

You might be interested in
What does Nicholas Carr suggest in his article "Is Google Making Us
Aleksandr-060686 [28]

Answer:

I believe the answer would be C

Explanation:

3 0
3 years ago
It is appropriate to leave sections of an application blank ?<br><br><br><br> True or False
sergiy2304 [10]

false because if you leave them blank then ther will be no use to add them in the first

3 0
3 years ago
A screen saver is best described as a
sashaice [31]
A wallpaper or background.
3 0
3 years ago
The rules that establish an orderly transfer of data between sender and receiver are called ____.
zloy xaker [14]
Protocols               is the answer                        
8 0
3 years ago
Read 2 more answers
Ideally, how often should you back up the data on your computer? once an hour, once a day, once a month, once a year. Please hur
Stells [14]

Explanation:

Every month or so

5 0
3 years ago
Read 2 more answers
Other questions:
  • Which statement is true
    12·1 answer
  • What type of html list will automatically place a number in front of the items?
    9·1 answer
  • What pattern is a heart-shaped pattern that captures sound from a single direction, ?from the front?
    5·1 answer
  • Given an int variable n that has already been declared and initialized to a positive value, and another int variable j that has
    9·1 answer
  • of the menu items listed at the top of the screen, which will be used to determine what the shortcut f6 means in primesuite?
    5·1 answer
  • Linux is a kind of software whose code is provided for use, modification, and redistribution. what kind of software is this?
    5·1 answer
  • What is alfred anderson in creole?
    6·1 answer
  • How does our behavior change when we know we're being watched?
    14·1 answer
  • Kerry is debugging a program. She identifies a line of code to begin execution and a line of code to end execution so that she i
    11·1 answer
  • I'll give you Brainliest if you can tell me the name of this class and the name of the game, LOL
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!