Problem:
Given a number, determine if it is Fibonacci or not.
Input Format:
t=number of test cases
followed by t lines having 1 number
followed by t lines having 1 number
Output Format:
Yes or No for each input
Yes or No for each input
Constraints:
none
none
Sample Input
4
8
75025
80000
11111
8
75025
80000
11111
Sample Output:
Yes
Yes
No
No
Yes
Yes
No
No
Explanations:
series is 0, 1, 1, 2, 3, 5, 8, 13......
series is 0, 1, 1, 2, 3, 5, 8, 13......
Solution:
for($i=0;$i<$n;$i++)
{
chomp($s=<STDIN>);
$a=(5*$s*$s);
$b=$a+4;
$b=sqrt($b);
$c=int($b);
$d=$a-4;
$d=sqrt($d);
$e=int($d);
if($b == $c or $d == $e)
{
push(@out,'Yes');
}
else
{
push(@out,'No');
}
}
foreach(@out)
{
print "$_\n";
}
Tips:
Simple logic- check if 5*num*num+4 or 5*num*num-4 is a perfect sqaure.
No comments:
Post a Comment