5

I would like to ask how can I retrieve out the list of fibo list and then check whether does the input value by the user is inside the fibo list.

    a , b = 1, 1
while num <= sys.maxint:
    fibo == a , b = b, a+b
    if num == (b +a+b):
        print "It is a Fibonacci number"        
        break
    else:
        print "It is not a Fibonacci number"
        break

Thank you!

user950472
  • 69
  • 2
  • 4
  • Have you read [this question and its answers](http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python)? Don't they give you enough information to solve your problem? – MarcoS Sep 19 '11 at 14:05
  • Yes, I did.. But I dont really understand... – user950472 Sep 19 '11 at 14:07

4 Answers4

9

Using a more sophisticated Fibonacci number test, you could use

def is_fibonacci(n):
    phi = 0.5 + 0.5 * math.sqrt(5.0)
    a = phi * n
    return n == 0 or abs(round(a) - a) < 1.0 / n

(This is probably the most efficient way to determine whether a number is a Fibonacci number, and it is most probably not the intended solution to your homework. I just included this answer for future reference.)

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • I wanted to know a name for this test. I found it as the Möbius test, from [Michael Möbius](https://web.archive.org/web/20190807010928/https://link.springer.com/article/10.1007/s005910050048). [Here](https://web.archive.org/web/20190807010735/https://www.fq.math.ca/Scanned/41-1/komatsu.pdf) is a discussion of the interval. Some Stack Exchange links - [link1](https://math.stackexchange.com/q/823247/674126) [link2](https://math.stackexchange.com/a/10002/674126). – bballdave025 Aug 07 '19 at 01:17
5

A pythonic one-liner

def is_fibonacci(n):
    return n >= 0 and (n==0 or sqrt( 5*n*n - 4).is_integer() or sqrt( 5*n*n + 4).is_integer())
hoke1606
  • 81
  • 1
  • 5
1

Possibly a not very efficient solution - using the close formula is more efficient (see Sven's answer), but you can do this:

def fibs():
    a,b = 0,1
    yield a
    yield b
    while True:
        a,b = b,a+b
        yield b

n = int(raw_input("please, enter a number "))
for fib in fibs():
  if n == fib:
    print "your number is a Fibonacci number!"
    break
  if fib > n:
    print "your number is not a Fibonacci number!"
    break

The fibs generator gives you the list of Fibonacci numbers. You can go through the list, and every number you can check if it's equal to the one the user entered (in which case you're done), or if it's bigger than the one the user entered (and also in this case you're done).

I hope this is useful, at least to understand Python generators.

Community
  • 1
  • 1
MarcoS
  • 13,386
  • 7
  • 42
  • 63
  • 1
    Thank you MarcoS! It really help me alot! Can I ask what if we dont use the def or the yield, will the codes still work? I tried but not really. – user950472 Sep 19 '11 at 14:32
  • 1
    If you don't use the `yield`, then you don't get a Python generator, so the for loop does not work. Also, you remove the `def`inition of `fibs` the code will not work any more. So, answer to your question is "if you remove `def` or `yield` the code will no longer work". But why would you want to remove them? – MarcoS Sep 19 '11 at 14:38
  • Erm, I want to try out other ways. Maybe like this: – user950472 Sep 19 '11 at 15:23
  • import sys #User input a number num = int(raw_input("Input a Number:")) a,b = 1, 1 if (a , b == b, a + b): True else: False while num <= sys.maxint: if num == True: print "It is a Fibonacci number" break else: print "It is not a Fibonacci number" break – user950472 Sep 19 '11 at 15:23
  • well, you're free to experiment other ways ... and then playing with a debugger you can figure out what happens :) – MarcoS Sep 19 '11 at 16:03
  • Thanks MarcoS, But can you help me with this? I dont understand why the output isnt correct: import sys #User input a number num = int(raw_input("Input a Number:")) a,b = 1, 1 if (a , b == b, a + b): fibo_num = True else: fibo_num = False while num <= sys.maxint: if fibo_num = True: print "It is a Fibonacci number" break else: print "It is not a Fibonacci number" break – user950472 Sep 19 '11 at 16:13
  • Well, there are a few things that look strange. First of all: what is `if (a , b == b, a + b):` supposed to do? I suggest that you read the Python documentation and see how `if` statements work. – MarcoS Sep 19 '11 at 16:25
  • hmm.. Can I ask you what is the yield means? Is it like a object to store data? Then why must fib > n, to print not fib number? – user950472 Sep 19 '11 at 16:42
  • I'm afraid you need to read the manuals :) A good explanation of yield is [here](http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained). Enjoy! – MarcoS Sep 20 '11 at 10:44
0
x = int(input("Enter a number: "))

A = [0, 1]

for i in range(2,720):

    A.append(A[i-1]+A[i-2])

bool=False

for i in range(2,720):

    if x==A[i]: 

        bool=True
        break

if bool==True:

    print "Found,Index is:",i+1
else: 

    print "Not Found"
jHilscher
  • 1,810
  • 2
  • 25
  • 29
Gaurav
  • 1