Problem Definition
Given a number tell if its Fibonacci number or not.
Inputs
- Number of Test Cases 1<= T <= 10^10.
- T lines follow and each line consists of N integer.
Sample Input
3
5
7
8
Sample Output
IsFibo
IsNotFibo
IsFibo
And my code for this problem is given below
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) throws IOException {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tc = Integer.parseInt(br.readLine());
for(int i=0;i<tc;i++)
{
int n = Integer.parseInt(br.readLine());
doThis(n);
}
}
public static void doThis(int n)
{
double one = Math.sqrt((5*n*n)+4);
double two = Math.sqrt((5*n*n)-4);
if(one % 1 == 0 || two % 1 == 0)
{
System.out.println("IsFibo");
}
else
{
System.out.println("IsNotFibo");
}
}
}
The reason that i use Math.sqrt((5*n*n)-4)
or Math.sqrt((5*n*n)+4)
is given in this link
www.fq.math.ca/Scanned/10-4/advanced10-4.pdf pagenumber 418.
how ever this is not working for all the cases and most of the cases fail which i don't understand why?