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
vitfil [10]
3 years ago
7

#The Fibonacci sequence is a number sequence where each #number is the sum of the previous two numbers. The first #two numbers a

re defined as 0 and 1, so the third number is #1 (0 + 1 = 1), the fourth number is 2 (1 + 1 = 2), the #fifth number is 3 (1 + 2 = 3), the sixth number is 5 #(2 + 3 = 5), and so on.
#
#Below we've started a class called FibSeq. At any time, #FibSeq holds two values from the Fibonacci sequence: #back1 and back2.
#
#Create a new method inside FibSeq called next_number. The #next_number method should:
#
# - Calculate and return the next number in the sequence, # based on the previous 2. # - Update back2 with the former value of back1, and update # back1 with the new next item in the sequence.
#
#This means that consecutive calls to next_number should #yield each consecutive number from the Fibonacci sequence. #Calling next_number 5 times would print 1, 2, 3, 5, and 8.
class FibSeq:
def __init__(self):
self.back1 = 1
self.back2 = 0
def next_number(self):
self.back1=self.back1+self.back2 # updated the back1 value to the next number in the series first
self.back2=self.back1-self.back2 #updated the back2 value with previous back1 value
yield (self.back1) # yielded the next number in the series since it is updated as back1 so yielded back1
f = FibSeq()
for i in range(5): # here i have iterated the series only 5 times u can change it as you like
s=f.next_number()
print(next(s))# here next is an iterator function for the yield generator.
#The code below will test your method. It's not used for
#grading, so feel free to change it. As written, it should
#print 1, 2, 3, 5, and 8.
newFib = FibSeq()
print(newFib.next_number())
print(newFib.next_number())
print(newFib.next_number())
print(newFib.next_number())
print(newFib.next_number())

Computers and Technology
1 answer:
saw5 [17]3 years ago
3 0

Answer:

Here is the next_number method:

def next_number(self): #method to return next number in the sequence

    temporary = self.back1 + self.back2 # adds previous number to next number and stores the result in a temporary variable

    self.back2 = self.back1 #Updates back2 with the former value of back1,

    self.back1 = temporary #update back1 with the new next item in the sequence.

    return temporary #

Explanation:

I will explain the working of the above method.

back1 = 1

back2 = 0

At first call to next_number()  method:

temporary = back1 + back2

                 = 1 + 0

temporary = 1

self.back2 = self.back1

self.back2 = 1

self.back1 = temporary

self.back1 = 1

return temporary

This return statement returns the value stored in temporary variable i.e. 1

Output: 1

back1 = 1

back2 = 1

At second call to next_number()  method:

temporary = back1 + back2

                 = 1 + 1

temporary = 2

self.back2 = self.back1

self.back2 = 1

self.back1 = temporary

self.back1 = 2

return temporary

This return statement returns the value stored in temporary variable i.e. 2

output: 2

back1 = 2

back2 = 1

At second call to next_number()  method:

temporary = back1 + back2

                 = 2 + 1

temporary = 3

self.back2 = self.back1

self.back2 = 2

self.back1 = temporary

self.back1 = 3

return temporary

This return statement returns the value stored in temporary variable i.e. 3

Output: 3

back1 = 3

back2 = 2

At second call to next_number()  method:

temporary = back1 + back2

                 = 3 + 2

temporary = 5

self.back2 = self.back1

self.back2 = 3

self.back1 = temporary

self.back1 = 5

return temporary

This return statement returns the value stored in temporary variable i.e. 5

Output: 5

back1 = 5

back2 = 3

At second call to next_number()  method:

temporary = back1 + back2

                 = 5 + 3

temporary = 8

self.back2 = self.back1

self.back2 = 5

self.back1 = temporary

self.back1 = 8

return temporary

This return statement returns the value stored in temporary variable i.e. 8

Output: 8

Calling next_number 5 times would print 1, 2, 3, 5, and 8.

The complete program along with its output is attached.

You might be interested in
in a particular factory, a team leader is an hourly paid production worker who leads a small team. in addition to hourly pay, te
malfutka [58]

Facilitate team development for successful project completion. Through coaching and mentoring, provide teammates with technical leadership.

Establishing best practices and habits will help the team maintain high standards for the quality of its software. Identify and promote the team's potential development and improvement areas.

#include<iostream>

using namespace std;

/*C++ Function to print leaders in an array */

void printLeaders(int arr[], int size)

{

  for (int i = 0; i < size; i++)

   {

       int j;

       for (j = i+1; j < size; j++)

       {

           if (arr[i] <=arr[j])

               break;

       }  

       if (j == size) // the loop didn't break

           cout << arr[i] << " ";

 }

}

/* Driver program to test above function */

int main()

{

   int arr[] = {16, 17, 4, 3, 5, 2};

   int n = sizeof(arr)/sizeof(arr[0]);

   printLeaders(arr, n);

   return 0;

}

Learn more about Development here-

brainly.com/question/28011228

#SPJ4

7 0
1 year ago
Queues can be represented using linear arrays and have the variable REAR that point to the position from where insertions can be
Levart [38]

Answer:

8

Explanation:

7 0
2 years ago
Which is used to identify the network portion and the host portion of an ip address?
qwelly [4]
<span>subnet mask is used to identify the network portion and the host portion of an ip address.</span>
3 0
3 years ago
Create your own Python code examples that demonstrate each of the following. Do not copy examples from the book or any other sou
natulia [17]
Ssfhooiyfbjkoyfvhkodd
6 0
3 years ago
________ is a command-line utility installed in the windows\system32 folder that displays information about your windows version
Ne4ueva [31]
Im pretty sure its SystemInfo...... 
6 0
3 years ago
Other questions:
  • What is working with others to find a mutually agreeable outcome?
    6·1 answer
  • What date does GTA 6 come out<br> A. 2020<br> B. 2019<br> C. 2021<br> D. 2022
    10·2 answers
  • You should process the tokens by taking the first letter of every fifth word,starting with the first word in the file. Convert t
    12·1 answer
  • You are planning trip to South America and and are worried about your devices with private keys being stolen. So you decide to s
    9·1 answer
  • When planning your website, what is one of the key things you should consider
    12·2 answers
  • What will you see on the next line?
    6·2 answers
  • The NVCC college professor receives frequent emails disguised as official business emails. They may be requests to reset a passw
    10·2 answers
  • A Computer Programmer who wants to be surrounded by a large number of like-minded coworkers should take a job with one of the na
    14·2 answers
  • S.B. manages the website for the student union at Bridger College in Bozeman, Montana. The student union provides daily activiti
    7·1 answer
  • How to write the algorithm to calculate the square of five numbers
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!