Fast nth fibonacci term generator

posted on September 25, 2009

Here’s some code to generate nth fibonacci term with some optimized recursion. Already generated terms are not regenerated. An array acts as a cache for the terms. Besides, integers are represented with char * allowing huge term calculations.

fibogen.c

~/devel/fibogen :) gcc -O3 -march=native -mtune=native -pipe -fomit-frame-pointer -ffast-math -o fibogen fibogen.c
fibogen.c: In function 'main':
fibogen.c:23: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result
~/devel/fibogen :) fibogen
Enter term to calculate: 5023
fibogen(5023) = 248560419649283678513006888062737004960134760988418307905373388505942986481060775195244084194714556882854137612073536664237785579120282065911785293337513886015023953145486883305558080183807994149392421109008029126965116981886956731180641266870009523965481202176011120065634469413284383112960712441923153798256342167123227820469383663417964899013751648010246443960641434024852096101024638451971960625796144677722737304551414781031490411571580459982050355471494276920439983581193010326994482515332997366158457073763985574602205889946564359072664645673996548528062294288463152531141939436109347821144918253984914858266331041933193284095453325649088264822747102089656780678368753206478819515483528828479426315024856137069121990789427595837059650018342996344614879040425051671211007345988321930553661789981247352356512907804456540716374221778890018510373603590988025872815600298159173656785237948221052073363317357008281570513166462291327747202547805549308377844086138173022395933493065635120665887076447216650309115602750732968703082884641717211142536157
Real Time: 0.22, User Time: 0.22, System Time: 0.00