Vous êtes sur la page 1sur 9

3/10/2014

How to check if a given number is Fibonacci number? | GeeksforGeeks

GeeksforGeeks
A computer science portal for geeks

GeeksQuiz
Login
Home
Algorithms
DS
GATE
Interview Corner
Q&A
C
C++
Java
Books
Contribute
Ask a Q
About
Array
Bit Magic
C/C++
Articles
GFacts
Linked List
MCQ
Misc
Output
String
Tree
Graph

How to check if a given number is Fibonacci number?


Given a number n, how to check if n is a Fibonacci number.
A simple way is to generate Fibonacci numbers until the generated number is greater than or equal to n.
Following is an interesting property about Fibonacci numbers that can also be used to check if a given number is
Fibonacci or not.
A number is Fibonacci if and only if one or both of (5*n2 + 4) or (5*n2 4) is a perfect square (Source:
http://www.geeksforgeeks.org/check-number-fibonacci-number/

1/9

3/10/2014

How to check if a given number is Fibonacci number? | GeeksforGeeks

Wiki). Following is a simple program based on this concept.


/ C+pormt ceki xi apretsur
/ + rga o hc f
s
efc qae
#nld <otem
icue isra>
#nld <ahh
icue mt.>
uignmsaesd
sn aepc t;
/ Auiiyfnto ta rtrstu i xi pretsur
/
tlt ucin ht eun re f
s efc qae
bo iPretqaeitx
ol sefcSur(n )
{
its=sr()
n
qtx;
rtr (* = x;
eun ss = )
}
/ Rtrstu i ni aFbnciNme,es fle
/ eun re f
s
iiac ubr le as
bo iFbnciitn
ol sioac(n )
{
/ ni Fbncii oeo 5nn+4o 5nn-4o bt
/
s iiac f n f **
r **
r oh
/ i aprec sur
/ s
efrt qae
rtr iPretqae5nn+4 |
eun sefcSur(**
) |
iPretqae5nn-4;
sefcSur(**
)
}
/ Auiiyfnto t ts aoefntos
/
tlt ucin o et bv ucin
itmi(
n an)
{
fr(n i=1 i< 1;i+
o it
;
= 0 +)
iFbncii?cu < i< "i aFbnciNme \"
sioac() ot <
<
s
ioac ubr n:
cu < i< "i antFbnciNme \";
ot <
<
s
o ioac ubr n
rtr 0
eun ;
}
Output:
1i aFbnciNme
s
ioac ubr
2i aFbnciNme
s
ioac ubr
3i aFbnciNme
s
ioac ubr
4i antFbnciNme
s
o ioac ubr
5i aFbnciNme
s
ioac ubr
6i antFbnciNme
s
o ioac ubr
7i antFbnciNme
s
o ioac ubr
8i aFbnciNme
s
ioac ubr
9i antFbnciNme
s
o ioac ubr
1 i antFbnciNme
0 s
o ioac ubr

This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to
share more information about the topic discussed above

http://www.geeksforgeeks.org/check-number-fibonacci-number/

2/9

3/10/2014

How to check if a given number is Fibonacci number? | GeeksforGeeks

Related Tpoics:
Find if two rectangles overlap
Analysis of Algorithm | Set 4 (Solving Recurrences)
Print all possible paths from top left to bottom right of a mXn matrix
Generate all unique partitions of an integer
Russian Peasant Multiplication
Closest Pair of Points | O(nlogn) Implementation
Find the maximum distance covered using n bikes
Given n line segments, find if any two segments intersect
Like

91

Tw eet

Writing code in comment? Please use ideone.com and share the link here.
20 Comments

GeeksforGeeks

Sort by Newest

Login
Share

Favorite

Join the discussion


Balaji Nakkella

2 months ago

The above program can only give correct results for 1st 22 Fibonacci numbers..
3

Reply Share

Argha Ghosh

5 months ago

i made it in C after see your question,check it and run it @admin and friends
#include"stdio.h"
main(){
int n,a,b,sum,i,k=1,c;
http://www.geeksforgeeks.org/check-number-fibonacci-number/

3/9

3/10/2014

How to check if a given number is Fibonacci number? | GeeksforGeeks

int n,a,b,sum,i,k=1,c;
int p[30];
while(k==1){
a=0;
b=1;
sum=0;
p[0]=0;
p[1]=1;
i=2;
printf("enter the number:-\n");
scanf("%d",&n);
while(sum<=n){
sum=a+b;
a=b;

see more
Reply Share

priya

5 months ago

PLZ ANYBODY CAN TELL HOW TO DO DIS TYPE OF QUESTIONS


Given the following code snippet:
void InsertNode(tNode** node, int i){
if(*node
== NULL){
*node
= new tNode;

(*node)->pLeft = NULL;

(*node)->data = i;

(*node)->pRight = NULL;
see more
Reply Share

Sudarshan Singh

5 months ago

Thanks Abhay Rahti ,we came to know an interesting property.


Reply Share
http://www.geeksforgeeks.org/check-number-fibonacci-number/

4/9

3/10/2014

How to check if a given number is Fibonacci number? | GeeksforGeeks

v3gA

5 months ago

This is not the most efficient algorithm to check whether a given number is a Fibonacci number.
Matrix exponentiation is much faster than this method.
http://www.quora.com/Algorithm...
4

Reply Share

Sumit Kesarwani

v3gA 3 months ago

Yo v3ga u r right...awsome..source thanks


Reply Share

rahul

5 months ago

This code is great example why you should think simple. These ideas should only be used if
they optimize your code. But i dont see this property helping in anyway from a programmer's
perspective. Its only increasing the running time and length of the code. Maths is supposed to
make our lives easier not complicate it by giving unproductive results.
Reply Share

devian

rahul 5 months ago

"increasing the running time and length of the code"


... compared to what?
this solution is ridiculously faster than generating all the fib sequence up to n, and the
code is just 4 lines
Reply Share

rahul

devian 5 months ago

dude...4 lines doesnt mean easier:).we are not here for cramming...calulating
square and all will take time...
Reply Share

sumit

rahul 5 months ago

tell me that if 100034355 is fibo number or not?


This algorithm produces answer in log(n) time. Dude this is incredibly
faster than O(n) algorithm. And so that you know log(n) time is because
of checking sqrt condition.
1

Reply Share

Anonymous

sumit a month ago

as Balaji said already, this is applicable only for first 22 fibs.


but yes no one can deny the fact log(n) < O(n)
1
indian

http://www.geeksforgeeks.org/check-number-fibonacci-number/

Reply Share

sumit 5 months ago

5/9

3/10/2014

How to check if a given number is Fibonacci number? | GeeksforGeeks


sumit 5 months ago

indian

square of number will not be in range.. it has limits...


1
Herman

Reply Share

5 months ago

If I were to be asked this in an interview...I would slap the interviewer :)


This shows nothing about your ability to code (it shows only that you happen to know the
answer or that you are a math geek)
6

Reply Share

The_Computing_Machine

Herman 5 months ago

To be good in computer science you must be good in math too.


Without math computer science has no existence.
5

Reply Share

Matth

Herman 5 months ago

I agree with you!there is nothing but math.


Reply Share

rahul

Herman 5 months ago

hahahaah...i agree with you..:)...this is not mathematics portal:)


1

Reply Share

Sandipan Manna

5 months ago

"A number is Fibonacci if and only if one or both of 5n2+4 or 5x2-4 is a perfect square "
Typo : It should be 5n^2-4
Reply Share

GeeksforGeeks

Mod

Sandipan Manna 5 months ago

Thanks for pointing this out. We have updated the statement.


Reply Share

Destined2workhard

5 months ago

logic part is all important !!


Jai ho @WIKI :)
Reply Share

bibhas_abhishek

5 months ago

Well implemented program


1

Reply Share

http://www.geeksforgeeks.org/check-number-fibonacci-number/

6/9

3/10/2014

How to check if a given number is Fibonacci number? | GeeksforGeeks

Subscribe

Add Disqus to your site

GeeksforGeeks
Like

47,447 people like GeeksforGeeks.

Facebook social plugin

Interview Experiences
Advanced Data Structures
Dynamic Programming
Greedy Algorithms
Backtracking
Pattern Searching
Divide & Conquer
Mathematical Algorithms
Recursion
Geometric Algorithms

http://www.geeksforgeeks.org/check-number-fibonacci-number/

7/9

3/10/2014

How to check if a given number is Fibonacci number? | GeeksforGeeks

Popular Posts
All permutations of a given string
Memory Layout of C Programs
Understanding extern keyword in C
Median of two sorted arrays
Tree traversal without recursion and without stack!
Structure Member Alignment, Padding and Data Packing
Intersection point of two Linked Lists
Lowest Common Ancestor in a BST.
Check if a binary tree is BST or not
Sorted Linked List to Balanced BST

660

Subscribe

http://www.geeksforgeeks.org/check-number-fibonacci-number/

8/9

3/10/2014

How to check if a given number is Fibonacci number? | GeeksforGeeks

Recent Comments
kunal
ay is a subsequence.. we are looking for a...
Dynamic Programming | Set 29 (Longest Common Substring) 35 minutes ago
kri
// Arithmetic based method x = x + x; // x...
How to swap two numbers without using a temporary variable? 1 hour ago
vishal
For structures , we can use memcmp memcmp(...
How to swap two numbers without using a temporary variable? 2 hours ago
Nitin Sharma
Mr.Ganesh Ram Sundaram has provided very cool...
Rearrange an array in-place 3 hours ago
jafar
I don't see how that will give us the desired...
Dynamic Programming | Set 27 (Maximum sum rectangle in a 2D matrix) 3 hours ago
jaaaames
the code is for longest contiguous string
Dynamic Programming | Set 29 (Longest Common Substring) 4 hours ago

Random Number
Fibonacci
The N Number
C++ Java
C++ Programming Golden Number

Numbers Number
Code Number
C++ Function

@geeksforgeeks, Some rights reserved


Contact Us!
Powered by WordPress & MooTools, customized by geeksforgeeks team

http://www.geeksforgeeks.org/check-number-fibonacci-number/

9/9

Vous aimerez peut-être aussi