Home > OS >  Is there a way to code the Fibonacci Sequence to only get one output
Is there a way to code the Fibonacci Sequence to only get one output

Time:11-02

Many of the questions and answers I’ve seen on here give an answer of multiple outputs however I want only one single answer can anyone help? For example with this code when you choose a number in this case ‘9’ you get ‘0, 1, 1, 2, 3, 5, 8, 13, 21’ what I want is if I say ‘9’ I just get an answer of ‘21’ nothing before it.

nterms = int(input("How many terms? "))

n1, n2 = 0, 1
count = 0

if nterms <= 0:
   print("Please enter a positive integer")
elif nterms == 1:
   print("Fibonacci sequence upto",nterms,":")
   print(n1)
else:
   print("Fibonacci sequence:")
   while count < nterms:
       print(n1)
       nth = n1   n2
       n1 = n2
       n2 = nth
       count  = 1   

CodePudding user response:

nterms = int(input("How many terms? "))

n1, n2 = 0, 1
count = 0

if nterms <= 0:
   print("Please enter a positive integer")
elif nterms == 1:
   print("Fibonacci sequence upto",nterms,":")
   print(n1)
else:
   print("Fibonacci sequence:")
   while count < nterms:
       if count   1 >= nterms:
           print(n1)

       nth = n1   n2
       n1 = n2
       n2 = nth
       count  = 1

CodePudding user response:

Try this:

nterms = int(input("How many terms? "))

n1, n2 = 0, 1
count = 0

if nterms <= 0:
   print("Please enter a positive integer")
elif nterms == 1:
   print("Fibonacci sequence upto",nterms,":")
   print(n1)
else:
   print("Fibonacci sequence:")
   while count < (nterms-1):
       nth = n1   n2
       n1 = n2
       n2 = nth
       count  = 1  
print(n1)

CodePudding user response:

You can create a list of lenght ‚nterms‘ and get the last element of it with array.pop. Or you can assign the current value of fibonaci to a variable and as the loop finishes print the last assigned value.


def fibo():
    while count < nterms:
        f = a   b
        a = b
        b = f
        count  = 1
    print(f)

CodePudding user response:

If I understood your question correct; then based on you original code, I suggest just making a function that print only the final number:

def final_fib(nterms, print_all=False):
    n1, n2, count = 0, 1, 0

    if nterms <= 0:
       print("Please enter a positive integer")
       return 
    elif nterms == 1:
       print("Fibonacci sequence upto",nterms,":")
       return(n1)
    
    fib_nrs = list()
    fib_nrs.append(n1)
    
    while count < nterms:
        if print_all:
            print(n1)
        fib_nrs.append(n1)
        nth = n1   n2
        n1 = n2
        n2 = nth
        count  = 1
    if not print_all:
        print(fib_nrs[-1])

Which would give:

final_fib(9)
21

Note, that I also made it possible to print all numbers by setting print_all=False. That is;

final_fib(9, print_all=True)
0
1
1
2
3
5
8
13
21

CodePudding user response:

You can use a function to recursively calculate the n number of the sequence:

def fibMem(n, mem={}):
    if n in mem:
        return mem[n]
    if n <= 1:
        return 1
    mem[n] = fibMem(n-1, mem)   fibMem(n-2, mem)
    return mem[n]
num = 9
print(fibMem(num-2))
  • Related