I am not able to solve the Amazon Interviewstreet challenge for fibonacci factor in php.
https://amazon.interviewstreet.com/challenges/dashboard/#problems
Fibonacci values upto 10^18
large int values seem to be the problem and bcmath is not helping…
My Code –
<?php
function s($k)
{
$x=$y=1;
while($y<=$k)
{
$z=$x+$y;
$x=$y;
$y=$z;
if($y%2==0&&$k%2==0)
{
fprintf(fopen("php://stdout", "w"), "%d ", 2);
fprintf(fopen("php://stdout", "w"), "%d\n", 2);
return;
}
for($i=3;$i<=$y;$i+=2)
if($y%$i==0&&$k%$i==0)
{
fprintf(fopen("php://stdout", "w"), "%d ", $y);
fprintf(fopen("php://stdout", "w"), "%d\n", $i);
return;
}
}
while($y%$k!=0)
{
$z=$x+$y;
$x=$y;
$y=$z;
}
fprintf(fopen("php://stdout", "w"), "%d ", $y);
fprintf(fopen("php://stdout", "w"), "%d\n", $k);
}
fscanf(STDIN, "%d\n", $t);
while($t--)
{
fscanf(STDIN, "%d\n", $k);
s($k);
}
?>
1 Answer