4

Hey I am trying to get a Fibonacci sequence to output with a single variable in the mix. normally if I was using 2 variables I would have it set up like this:

nmbr1 = nmbr2 = 1
while nmbr1 < 100:
  nmbr1, nmbr2 = nmbr1 + nmbr2, nmbr1
  print (nmbr1)

but how would I get it complete the sequence with only one variable in python?

4 Answers4

4

Since nobody mentioned what sort of object the variable should be, here's using a list ;-)

x = [1, 1]

while x[0] < 100:
     x = x[1], sum(x)
     print(x[0])

1
2
3
5
8
13
21
34
55
89
144

If you really want to be sneaky, you can use the closed form solution for the Fibonacci series by approximation with the golden ratio.

def fib(n): 
    return int((((1 + 5 ** .5) / 2) ** n) / (5 ** .5) + .5)

f = c = 1
while f < 100:
    c += 1
    f = fib(c) 
    print(f)

1
2
3
5
8
13
21
34
55
89
144

This only uses one variable - n - and it calculates F[n] in constant time. Run a loop and keep calling fib successively.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • @JoranBeasley I've upvoted yours since it is most likely what they're looking for :) – cs95 Jan 28 '18 at 01:50
  • unfortunately i can only assume its for some homework or something ... and i dont think they necessarily want all fibs under one 100 but rather the 100th fib – Joran Beasley Jan 28 '18 at 01:51
  • 1
    @JoranBeasley It very well may be... although the question doesn't explicitly mention what exactly op is trying to compute... the `while nmbr1 < 100` in their Q may say otherwise... though we'll just have to wait... – cs95 Jan 28 '18 at 01:53
  • Why `x[:]` instead of just `x`? – Stefan Pochmann Jan 28 '18 at 02:13
  • @StefanPochmann I wanted to use the same list object instead of recreating it in each iteration (so it looks more like what OP wants). – cs95 Jan 28 '18 at 02:18
  • Not sure what you mean with "variable" then. Either way I see only one variable there, namely `x`. – Stefan Pochmann Jan 28 '18 at 02:26
3
def fib(n):
    if n <= 2: return 1
    return fib(n-1) + fib(n-2)

print fib(12) # the 12th fibinocci number

maybe ... it works a bit different then yours and it will fall apart with big numbers probably

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • its sort of a trap ... from an employers standpoint this is likely to not get you the job, since it slows down alot as you get to bigger numbers(try to get the 100th fibinocci for example) – Joran Beasley Jan 28 '18 at 01:59
  • Perfectly at home on a website called Stack Overflow. – 101 Jan 28 '18 at 02:05
1

This is an interesting solution. The memoization component is courtesy of Efficient calculation of Fibonacci series.

import functools

@functools.lru_cache(None)
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)

def fib_yield(n):
    for i in range(n):
        yield fib(i)

list(fib_yield(10))  # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
jpp
  • 159,742
  • 34
  • 281
  • 339
1

yes definitely agree with @joran-beasley

fastest and advanced technique for this is Memoization technique, though it is complicated. Memoization avoids computing already computed values by storing them, here we can store it in the dictionary with its positions as keys.

I learnt this from a very old answer in SO https://stackoverflow.com/a/18172463/5334188

be_good_do_good
  • 4,311
  • 3
  • 28
  • 42