-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIsFibo.java
More file actions
35 lines (27 loc) · 887 Bytes
/
IsFibo.java
File metadata and controls
35 lines (27 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class Solution {
// Complete the solve function below.
// A number is Fibonacci if and only if one or both of (5*n^2 + 4) or (5*n^2 – 4) is a perfect square
static String solve(double n) {
if(isPerfectSquare(5*n*n - 4) || isPerfectSquare(5*n*n + 4))
return "IsFibo";
else
return "IsNotFibo";
}
static boolean isPerfectSquare(double x){
double sqr = Math.sqrt(x);
return (sqr == Math.floor(sqr));
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int t = scanner.nextInt();
while(t-- > 0){
double n = scanner.nextDouble();
System.out.println(solve(n));
}
}
}