Calculate the Fibonacci Sequence with Perl
Fibonacci is a number sequence which is recursively defined by:
F(n) = F(n-1) F(n-2)
You begin with the numbers 0 and 1, and continue by adding those together, getting a third number (1). Now, you add the last two numbers (1 and 1), and add the resulting number (2) in the stack as the fourth number in the Fibonacci sequence. Here is a Perl script for recursively calculating the Fibonacci sequence:
# The list in which the Fibonacci numbers will be stored my @fibonacci = (0,1); # The number on which to start calculating my $n = 1; # Repeat for the 1000 first Fibonacci numbers while (@fibonacci < 1000) { # Assign $n the sum of the last two Fibonacci numbers $n = $fibonacci[@fibonacci - 1] $fibonacci[@fibonacci - 2]; # Push $n into the Fibonacci array push @fibonacci, $n; # Add the result to the Fibonacci list }
The 1000 first Fibonacci numbers will now be in the @fibonacci array. Calculating the Fibonacci numbers this way is very fast, and you can probably calculate a nigh-infinite number of elements in the Fibonacci sequence this way as long as Perl can handle a list of that size. Beware, though, that Perl might start rounding the numbers to infinity.of exam breast moviescript movie databaseclip movie art theatermovies columbus ohiompeg players moviemovie new chuckymovie noonerold titles movie Map

Well man, check this out - my version of fib. It’s both fast, and memory-efficient :)
PS: You gotta need Math::Pari
PPS: … and that module DOES kick ass.
#! -*- perl -*-
use Math::Pari qw(:all);
setprecision(10005);
our $sqrt5 = PARI(’2.236067977499789696409173668731276235440618359611525724270897245410520925637804899414414408378782274969508176150773783504253267724447073863586360121533452708866778173191879165811276645322639856580535761350417533785003423392414064442086432539097252592627228876299517402440681611775908909498492371390729728898482088641542689894099131693577019748678884425089754132956183176921499977424801530434115035957668332512498815178139408000562420855243542235556106306342820234093331982933959746352271201341749614202635904737885504389687061135660045757139956595566956917564578221952500060539231234005009286764875529722056766253666074485853505262330678494633422242317637277026632407680104443315825733505893098136226343198686471946989970180818952426445962034522141192232912598196325811104170495807048120403455994943506855551855572512388641655010262436312571024449618789424682903404474716115455723201737676590460918529575603577984398054155380779064393639723028756062999482213852177348592453515121046345555040707227872421534778752911212121184331789335191038008011118179004590618846249647104244248308880129406811314695953279447898998931691577460792461807500679877124204847380502773608291559913962448914943560683462529064408327944642680888989746046308353537875042061374757606883401879088192559117973574464190248537871146194090191913688035110397638436041281058110378698951852014697045642021763892890884446377826385893792440046028875405398460156061705223615090385775410042193684987254271850375215557693316723004778269866662446210678464272486385274578213410067985645305271124180595972849455195451310172309750871496529436282902540012047780324155464489988706177998190033606562243886409639287753517266295971438227956307956149523015444235016538917278640913041979397111356282139367457681174922067562108887818873671671627622623379877111539509682982890683018259081401003895509723261508452834587893607346396117236678366571982607921440289119008995584241522495712918323216741189975720139403788197728015288723418668345418382867300274315320229607628612524761028642346963020111802691220236015810127628430541861717618575140690101561629091763981267225965596282349067854624161857945584442659612858937564854974803490110813557514166474621951830235525956886569495816353036195574536832235265007722422582873668753404700742232661451739766517420672644476219618024220397983536829835024662680305467687674469001869572099585891983164402516209196461851057442482740872298204109437109922361752853153022121091762951208863569597169079462572603250897522297040434128808223321533901195515665140790221756461654212957878042231382078553676907726666431316593195462068720646450914872744082488128177653475168679073591862464426874641991499778939913129472014591999678257620639485262503594282864024622559103789556345382831782355983912962511600369101312659057197182001817243605955127578519983299892856386044587104693349518653903308428042182726036389445415780244174574723414697299996312510945622746959743313905497801628876810654967562756493383488845926982941631401470509141417954535093868764523909372306624190671584760292185470204202383804367213501946179150579154936284590867887709863106792607614583383516922029219901101296073586082944731440797201471015218046346250032264096871672963540969636219832048850465433443803786691927572175750574034787186060267180224742047834253180940526988056615337534872773026542125606463481386346689646871290637011627062170994667015199335574248981167273508265781724812649127907144250485223405560573120864698856746034511488116745565359920634787280265752554024873596622892873895341062544984820943340027649566257313012986868360780082035610679011754491733115104587831647941683545966745646230513852185991884480001121253357348715847944908169635303946872530537889777105440549557494671967073455522815183424102653868967505983299961872049235688535145553580038383814076104409224649647826522086543833690246120472557870908649235399515073780835273005095702764926293166727667520471557985345977264323716791807279963676916552898249196187408611111922759468652269660989899373621790713926965635625772507292168406789307638883891428533364743678983664741817149700533136079794881324210720612800521634225331990839873746321891445776218415576455440727336896306512345682353819585333310447693766227437059838432638104031372624456414311997529368471041185707435156153121007354626195038244794777444936516114319489148096859756147044319685335325156154124038860801085100316625006035068188234382038597807689450066597604900287359368833895919090609182062762324374091359957327323492119140006891940227050362692013131070406957762348299882549652830427113552788078142077417636467613606700076093600349616441182193688405289280437541068020063605763328830618278789631280638564557096482902100637765037994014972457588431179148564043331412437618115617610064935398259457442077414739481287133491784051731314795712171913306821407199566408926726929709789953277707020910545964845813996977073936560929194915253028687181018766424874866677410333142980118142113404977597160874363025220088076297608145048812328580449564543054482241701315776774249872702136127303334864446555355115947985407524638294094647910241214110079841768852074175816866685236768271941563296591074286439223790075954292600151119507591407104542898638264345112880256618361009001798437410242372138671463077918701580601473454046628330640846803107482885374308110232959222866460497088081881382291227974605207903656336068965050865347715180112086404907454385824972916266688339705987827149573979159728789960460942339343147245678240362546258331799055198384406367447136545587712746625309599718249265500601211851349099588701762385901137098651871063745828360227282437494150525621373966027152104943889118643910719220905660629767823538602393171668628849789797131168501668218859005543951667044885825147298761508342274784875202870136597569865425995024573763920996715503175435608213942639335069543895845273038032679542569478158672222382817996611206722121974343566110870807121790585816369282874278588756271209640778958251490154151150206004841453258003618084586849885181213322826645744539613802919890239909560327983028252250514565613286625233149387763902128843347743600022008436966051618330867674984728236777712937028630012746380859029629388486292179050941440748111338261384419816096389059502213009285621083551051819037426377671829531992082635920418830617171066477545076046545526595474428625593643343246884236640360576282549488633769443691878556287094819999814446640611852595322247665596663397650786252401300740576895657333880894615894209522511731675059724725019996464671943101446767666488163051556386728525260866053179163416009025577462318711754944345129894001032733451543078419681900654902243073746018243992590455318263274187937214542685385246309506608759866331622147392862843439588681127831024216216272525377713949613612023383788350054459748317398358290699892488838802439571720274732165738144473029542782537484193302757512411837086577766834858418031262665663871512441794275312619570031263099649128917308495858714456575012169629067027043634591758659823420064952444104389290210724901025976861742688879014488534702925723598366467291967392752654451513831944790876610417329494847630221585469896739047929585379873966498359990055790001209193226269267260498999029616106580358059503650317500980148703759670236720654455452034348090711433177111565945821239163870342110965158614182011527173980385944359903374623511262889712962004400285090811085854691767423204198958914416475608737437889611273783651604889992637566840549820306715821454672506578138669482476044440232525542386170897005908382640080199973113330355132819073123957956367609020607130202631789178057437221738117878942736029691400367329912944065886687485978928548251028718116869681839097403047228063478278072328803969101020982423395840024039992101398993280607017273858078820140389010640324697455264654648988792609617811085027594466295037041418205012737196335906096362014788490634004776095196686469008285168628127225442192045648467564561805595319215542169878303497746337554270447801823423470183720130924019804995170555850855633194076699011602125231066738218756931954210595004463461482435566883788236919317220596037557485489127733932255449007691721052830206081796515555089482306641528151763355029951076094232593355420117532923190993553854109924787971418510140548139956281686249930726143733067436121174844851963306141051476690831541085843259962298350172226235315463441912313129573909489785426412161270915589248290621339674842275963379276470666089557663386794574578362073281665397139765088770333517245798613928693697950296817580792952084072204120434347889405269752673087863904581547672334779623562484967311562100683389031272520860073314862169533097555602571558472908370448944723427484585116831862712257327433406561443431067852926514613461278217082177361714856771765612046066828171007819470774522690239258528319904255786227088629203054618051076542086519324534878074911272245722781566388671411800762974017973226307963917148846608839417133934445862854614827697655779511777215994774089404063336697138839819309605964986396353158536597112594460213655544703254815676148637556546364238393905601032175831442576526759364625451257400030003658595154599871581898392815268857231514270888557967660809094054203891600851640424046891612606900673162944370984073599799458707078393624266390330759490798222988488903660677176825808036356376427520133185698827355086349032108187742207374304232808116438689424089655519210833897290797566525390960278300380779918626134063732334132743928051385734277742932623783853713653839905529159954365575189219232344377362189093031577382448212196283945372385109575798526307189458456501610850338136280215635920437706615246111276326328038449090065153134785199530350616028543214286174377257196720749301142684093401668655084605540955866223673338064657706137477598142718014806098149197790272953752173568864764964378612351406391276064616394387271345483928774525174123086614592740762550303408120101151897654477126903127810531542085291895208111390191968177780752415991327760357237118318882234501846265595422760658875309501803303389930198321476678754316664604914038966948001408752373439969668032636520169252393125692982364513777150426927509805083616676427670444905174535787590107518312071589093970182045124509100060351420989396669670906476705215741844386091635646120247415065613567370826131692735777328669724987360203975655992613405190865302756120147727858171296617483152374837946916969002837795305868220274443172871198310542272466440070533557183197804628923266420530393318152641230487674952380990631659376725300841896802113091782612596554344996192839189446809302208396426953787080360765139099125720784885283197349719645601207077256783324025056532149866123931699303999588387864520344714214672850741660660228672499715072719408488929519979999017100827099551171718691531818530666145055508335168629338735356123407002400768974976764675206881550318955624437661418001747732547241833215981004539785406437995207590197811821718207859346912292214006091638425477851985392212423352872848767002820408172642998356305959363044759664485475073140171069399593108277181006536723204455769510941253968782177042060415372094136091136931209833729977212324459046478141961852586046759129643599632652916557777553490416927439421269578462133509387100952303955633980517636808158550218036489655741050119539675070286124489018044047648796462510116832464143766386013872129293641931900130985802194323730527344322148342723675533466559512537096024913153655806352078931107890462867751354606995831571771820233274969113516959054278372175650802084666597148854942379392209702528039500871981841532431179973214736752463767176901625859002293307096563428969281137304930818156309432392515689391505251389103312030383280584359770978187465606293038444951800619314309810107220875537375452383190568984092957379469474171968276902632423859440252684815473890919637300593184670690251369099490822596394717533414572032321124084612721322605629935468915954755011151293309505126452963542339957141742456630862091382465250069953623049043489947922734976440929610393775086839390238662409004321028587696895090476425402876619157116272393566046213701616917539041181065893667698094241983251127300680068793416578673967348460031502347703025382461323445528288432150258346837494286301864838498283219399973456316477185147196477896985498392923045445466774926242768725242327589341240652604501109791611461675009225984622725983461389788146851766389679985483279019688792681152705694351255243855730450792174402621609728130687923377509050685261979390352380395419263845174186843319119489435003150767534830445611413005613662867130498343994667168061283071015182319485287329656932562736043490118194117892054885852644444309189015160931424121372798086164738793864164749815351223803431226108496266234304851369569267275400304088358330023364651504723209914994127816448868890207024380976396605520035336197019304908780143981960699860537213510477593705843894647867047401733004428149291080744446869633514987462892818567580130783935480207117238723631336732337297847911099229056529457899883212316060917357829239434563109022001133210849993839482055974811865528699074290503353892413971957618939003494604568285514377438818424182759881188607410087296772008692904559866058478037318453797499842265131211156802846708521179021124073814405787863184088095367185527295992011928097215239783185963899017575720553269198108085275400918016065588694412596508905127129590859849763972922723426289715469399069511543109827684785788035080682799476923389625869584844692194860392550460276572144489927619076768030531356395290151770957103109846904695632920661258776840199016065202818366051487715413420504544873338119778170900311415084606333318494470578494051772495897750925055314545703022257565413469086204890304669130845686220793590565925003873978799669479235279761914708305202907459293629476436872010421989442388331829894334104075845104192672916969360828955606043294572479985280967270175474956490032764017904806450687598515802585312803110755081835034088392545700782533919133297544859353207346069073374680981582837738904294316558163144679382480799717387817103461596039110922570268178241221680244272341411408601211384937118329376695466417137828253588568960827693656265138582963202195725453937337347478342378629245382697891608555797992162894190495258100385206232984137354866373230939337932036453271575775012177124871253578655947092678083642175492760784324895440513453991927836493755769109943580770316784094966398062552448741324701850375508682802142246717318154962441275269180397684509454553105810087990050488807822731653416266011611764189206204165226827382551458739857860599234617856873406304771795079747778736148830527475884810128975283435372271046865397314579409261383603485559443477797188865697041145151766751276403010934413033485053637897033466560926152956265862652057864587320904204263796259753230524889749733877808123569398333308349701691959402923564316900298391442196501784690349490245086547736394517298891761675427973701319681709154633083383481341042238983325726754645275069513327400442406550487799954720121480854059444072695561′);
our $left = 1 + $sqrt5;
our $right = 1 - $sqrt5;
my $i = PARI(70000);
print (($left**$i-$right**$i)/((2**$i)*$sqrt5));
Comment by alpha — July 4, 2007 @ 11:21 pm
Really nice, Alpha. I didn’t think about using the fact that the ratio between two consecutive fibonacci numbers is related to the golden ratio. Clever.
Comment by Tim — July 12, 2007 @ 9:09 pm