Is Fibo code in C#

Problem source: https://www.hackerrank.com/challenges/is-fibo

You are given an integer, N. Write a program to determine if N is an element of the Fibonacci sequence.

The first few elements of the Fibonacci sequence are 0,1,1,2,3,5,8,13,. A Fibonacci sequence is one where every element is a sum of the previous two elements in the sequence. The first two elements are 0 and 1.

Formally:

fib0fib1fibn=0=1=fibn1+fibn2n>1

Input Format
The first line contains T, number of test cases.
T lines follow. Each line contains an integer N.

Output Format
Display IsFibo if N is a Fibonacci number and IsNotFibo if it is not. The output for each test case should be displayed in a new line.

Constraints
1T105
1N1010

Sample Input

3
5
7
8

Sample Output

IsFibo
IsNotFibo
IsFibo

Explanation:
A number is Fibonacci number is (5n^2 + 4) or (5n^2 - 4) is perfect square. 

class Is_Fibo
{
static void Main(string[] args)
{
int T = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < T; ++i)
{
var N = Convert.ToInt64(Console.ReadLine());
bool isFibo = CheckisFibo(N);
Console.WriteLine(isFibo? "IsFibo" : "IsNotFibo");
}
}

private static bool CheckisFibo(long N)
{
Int64 temp = 0;
double sqrValue = (5 * Math.Pow(N, 2));
return Int64.TryParse(Math.Sqrt(sqrValue + 4).ToString(), out temp) || Int64.TryParse(Math.Sqrt(sqrValue – 4).ToString(), out temp);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s