Getting TLE. plz someone reply me , what is wrong with my code and algorithm .

according to problem description, all fibonacci primes will occur at prime postions so i calculated first 22000 primes . then i use the following formula (

http://mathworld.wolfram.com/FibonacciNumber.html) F(n)=phi^n/sqrt(5) where phi=(1+sqrt(5))/2 and calculated first 9 digits as explained in the posts. There is some precison issue also in fun2() which gives wrong output for inputs 22000,6879. so how to avoid precision issue . here is my code .

Code: Select all

```
#include<cstdio>
#include<cmath>
long double a,c;
double fun2(int n)
{
int k;
long double v,d;
for(k=0;;k++)
{
v=(long double)n*log10l(a)-(log10l(c)+(long double)k);
if(v<9)
break;
}
d=powl(10,v);
d=floor(d+0.5);
return d;
}
int fun1(int n)
{
int i;
if(n==1)
return 0;
if(n==2)
return 1;
if(n%2==0)
return 0;
for(i=3;i*i<=n;i+=2)
if(n%i==0)
return 0;
return 1;
}
int main()
{
int prime[22010],count=0,i,n;
long double fib[22010];
a=(1+sqrtl(5))/2;
c=sqrtl(5);
for(i=1;;i++)
{
if(fun1(i))
{
prime[count++]=i;
if(count==22000)
break;
}
}
for(i=0;i<22000;i++)
{
fib[i]=fun2(prime[i]);
}
while(scanf("%d",&n)==1)
printf("%.0llf\n",fib[n-1]);
}
```