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!

2
  • Have you read this question and its answers? Don't they give you enough information to solve your problem? Commented Sep 19, 2011 at 14:05
  • Yes, I did.. But I dont really understand... Commented Sep 19, 2011 at 14:07

4 Answers 4

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

I wanted to know a name for this test. I found it as the Möbius test, from Michael Möbius. Here is a discussion of the interval. Some Stack Exchange links - link1 link2.
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()) 

1 Comment

I don't know why this is not upvoted! This code is awesome!!
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.

9 Comments

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.
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 definition 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?
Erm, I want to try out other ways. Maybe like this:
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
well, you're free to experiment other ways ... and then playing with a debugger you can figure out what happens :)
|
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" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.