Find the nth Fibonnaci Prime, in the shortest code
C, 66
f(n,a,b){int i=2;while(a%i&&i++<a);return(n-=i==a)?f(n,b,a+b):a;}
C, 85, 81, 76
f(n){int i=1,j=0,k;for(;n;n-=k==i)for(j=i-j,i+=j,k=2;i%k&&k++<i;);return i;}
borrowed code style of simplified prime number check from @Gautam
self contained C function (no globals)
Testing:
main(int n,char**v){printf("%d\n",f(atoi(v[1])));}
./a.out 10
433494437
./a.out 4
13
Mathematica, 59 or 63 bytes
(a=b=1;Do[While[{a,b}={b,a+b};Length@Divisors@b>2],{#}];b)&
(a=b=1;Do[While[{a,b}={b,a+b};b~Mod~Range@b~Count~0>2],{#}];b)&
These are unnamed functions which take n
as their input and return the correct Fibonacci prime. The shorter version uses Divisors
. I'm not entirely sure whether this is allowed, but the other Mathematica answer even uses FactorInteger
.
The second one doesn't use any factorisation related functions at all, but instead counts the number of integers smaller than n
which produce 0
in a modulo operation. Even this version beats all valid submissions, but I'm sure just posting this answer will cause some people to provide competitive answers in GolfScript, APL or J. ;)