1

I'm trying to store fibonacci sequence as rows in a csv file. I need to store large numbers but it isn't working. I tried to use Rmpfr, but I couldn't find a way to do it properly. The other good method was to create a long list but I want to save each number in a new row.

library(gmp)
fibonacci <- function(nth) {
    fib <- c(0,1)
    for(i in 3:nth){
        fib[i] <- fib[i-1] + fib[i-2]
    }
    as.bigz(fib)
}
N <- 15000
write.table(as.character(fibonacci(N)), "fibonacci big.csv", 
col.names = "Fibonacci Numbers",row.names = FALSE)

I see the output becomes the same from 1449th entry. as.bigz requires the number to be a character to avoid double precision issues. Is there a way to get output for larger numbers?

Edit: Edit 1: Mr Marek Fiołka made me realise I could have converted the initial vector to a bigz type vector to avoid conversion errors.

Edit 2: As pointed out by Mr Rudolph, I've removed the redundant conversion into bigz type.

The edited code:

library(gmp)
fibonacci <- function(nth) {
    fib <- as.bigz(c(0,1))
    for(i in 3:nth){
        fib[i] <- fib[i-1] + fib[i-2]
    }
    fib
}
N <- 15000
write.table(as.character(fibonacci(N)), "fibonacci big.csv", 
col.names = "Fibonacci Numbers",row.names = FALSE) 
VADeR
  • 27
  • 7
  • Does this answer your question? [long/bigint/decimal equivalent datatype in R](https://stackoverflow.com/questions/2053397/long-bigint-decimal-equivalent-datatype-in-r) – dario Oct 30 '21 at 08:32
  • @dario No. I tried it. It's losing precision after conversion. – VADeR Oct 30 '21 at 12:16
  • The edited code contains a redundant call to `as.bigz` when returning the result: the number is already in bigz format. The conversion is redundant. Anyway, this implementation is fairly inefficient. You don’t need to save all intermediate values, it’s sufficient (and much more efficient) to just save the two current values. – Konrad Rudolph Oct 31 '21 at 19:50
  • @KonradRudolph I didn't want to use recursion. That's why I went with the easy one. – VADeR Nov 01 '21 at 15:16
  • @VADeR My comment is unrelated to recursion. – Konrad Rudolph Nov 01 '21 at 15:16
  • @KonradRudolph Yes, I was referring to my previous code that I used to calculate those numbers. Sorry. – VADeR Nov 01 '21 at 15:18

1 Answers1

1
library(gmp)
library(data.table)
library(fs)
library(utils)


n = 1000
fileName = "fibonacci big.csv"

fwrite(list("0"), fileName)
fwrite(list("1"), fileName, append = file_exists(fileName))

pb = winProgressBar(max = n, width = 500)
on.exit(close(pb), add = TRUE)
fib0 = as.bigz(0)
fib1 = as.bigz(1)
for(i in 3:n){
  fib = fib0 + fib1
  fwrite(list(as.character(fib)), fileName, append = file_exists(fileName))
  setWinProgressBar(pb, i, paste(round(i/n, 2)*100, "%"))
  fib0 = fib1
  fib1 = fib
}
close(pb)

Note! For large n(15000), the calculation will take several minutes. The file can also be very large. For me, with n = 15000, it took about 2 minutes and the file was reduced to 22.5 MB. But we managed to find 15,000 elements of the Fibonnacci sequence. That's him.

180356212817232358527136269558393274270479577287959770214783776455882580176808282935116449287253001675278567205597341664270703993136851989859494809191272208168671602010135312495296553662790352582033423080689757992520655785610316941531173588408998619812807392167094455165363355259485592278979388870298110870050912172411703995878726547708126588747557108886477742159519675726841476567233617069303168933729769224839193682942110633367322841686836744207977400786184404283243942571683714924012584054120950619420879245008334171878098209414559267738476048405512373264705915648256421029922854826089454770481653365776765021018110912210918296748113016817314353185049893148191072970004722127877580853512230597663125482412037507790122452840808247238862266940374952733301348535189028427982115508256085629078838285854519748549211253115363900979738960948772589014559911367102564396479380119671836661458473444680552841046388920629103926658110037230386547506816712866616340383491227279709660200771238944743897531289959511150305531873160607872566720751381188951153762201432960669039121243742888978686571778654570582578356003787537920689451735958244288072349086881269456351988300299442324421142440230312335818179554507414851349917896307630882698214864014344956780348499120670367075038565963319741348415248138037342603113034434198363994519415494026021710622105031596662576344941382709460665227123428132640271682951472209368453748754510035741638613590279538796566181871895713339174456728394471357415996934436202565485732749272347348991793493320243181956465786389970599533658454386144770652584880176519203467064567384636418219255416950945755037995377546451320680325530900980211314010182766809827492194845387482751225482662997765661866262382112219008183345652570682633899549560294736097218086788785294700336993482784319083980440026327099112563241621005721911339313568395785607613027063516725870186041444932874242502339316433484868302535616417673857358757000197098734066555060275659441540730179880840068680711039092402469979537402038522869532858210224421954879125281803617171486680299882175622463954938841161683824017764172124148438928242788323301450705633955505182591808014745581257657569980476219657313856171171041106956687416108450931365450698373196534957320986723454445945232416845732305494189207716358085853199984634824143123729540264742981573020172237026058046740201545250287662018382140146169998065636288937211877886621661690578651489040221004254744005525689460483194423394923570500857169107685204978377809144999120496376447004389621794981767672901579794816635242858841182877011991980777649470507584848234893126914722042071134658473351945026030226538176505680011883028482299815999170838410236573499975247752075137709072936793331605370102620992579010729955042839150039536988476316814658001111401786786920802580057225089821741914525126541381743907218686111921778056456742847181643346546263899570276608898254139702386798819800561168979805761661085945938400171610214159378775050985949240013649311708619468421748433514761538254173535727874452760668054213182096783214642043675647412439936425441952983174011449159555969819986536279264099966926245798558475510001

Update

If I understood correctly, your task was to generate the Fibonacci sequence and save it to a file.

To do something like this (especially if you plan to compute many sequence elements and it can take a long time), it is a good idea to optimize your code immediately.

My optimization was not to store in memory subsequent elements of the Fibonacci sequence, but to save them to a file from the time after the calculation.

Second, my calculations are carried out on variables of the bigz type from the very beginning. Look at this:

fib0 = as.bigz(0)
fib1 = as.bigz(1)
[...]
fib = fib0 + fib1
[...]
fib0 = fib1
fib1 = fib

As you can see, I use bigz type variables all the time.

Besides, I used the fastest write-to-file function I know, which is fwrite from the data.table package. Don't miss the append = file_exists (fileName) check. During the computation, someone (yourself, the system, whoever) could delete the fibonacci big.csv file from the disk.

The rest of the setWin ProgressBar form is just a nice addition to give you an idea of how far we've gone in the calculations.

Update 2 If you are not interested in all the individual elements of the sequence and you do not want to save them to a file, you can simplify the calculations to such a function:

Fibbonnaci = function(n){
  if(n==0) return(0)
  if(n==1) return(1)
  fib0 = as.bigz(0)
  fib1 = as.bigz(1)
  for(i in 3:n){
    fib = fib0 + fib1
    fib0 = fib1
    fib1 = fib
  }
  return(fib)
}

For me, such a command Fibbonnaci(1500000) after a few minutes returned the following result:

Big Integer ('bigz') :
[1] 7978152357084244271455795748546353657353036219026221553729371708361852094497492432372675479154234401123900819608848632256850656022832685846865512834248264136619310144699757142503755505787577011441837539305310082770561767653026614841722357104617416568430516986804701547266424311388508909510859909306156606356060124029989757457910402332494190019588589260126899624535866658153620006930663864227167458567156619941406897261505614122875904721405866073809483137868986693033082627192630744655960574209593638955067301772641693901254284601277877781663187770447609158202546101965620958699964872970125877892788941865944041065964067531250301867585512017937042447446580980999794388811800533281722964070553604364440734142106704829177883802989822805015138996430876040162233784775621828570352732455792702407132294480936359553172094579108102610741177284887408548776112840332812543543083179785589682537766629093425749356078487412864923325470824581933556451767078767289414663285879344553275757013403649533436680104377914385088022746031352288306798727251774777958673136588118607870966813734806798298020564588466919577292269603623652799915179135487870131404195533158117907861395988475174610779466910383555525872678180248037465200704940093492478141658555991669073514179727479273744706001402330146475741873639387647719590383853155791159229465126337578632084428418025538815052235412143627283437519947653931049237873850906013485119248697859610171329975348534178282955560202492335866731949565292516317474337386005133875163593804353820396861158806749119766603652624478057159874401616209302049099335453562378959693998382824304497361188135943267639043676895482921930422688896208658456255975518260196531102646721686276829249814900308108649276780208921818734762005271039080952502003567152621862311033110861710896844633870926658806914254597463450020840823515058645892768455016114677451022785885768498890089783130498786977922877678448212587918844862304262091745543803616082099686900781002624883823524728258148539952535781560245489715501528718778057478414378096567089319928796235326118743137948966729338473278468907984707348072377155981098107808876105298434390624033599799538565532628608033065649144743552252406437154407890625829283696650384501720440978234821194487273181502520185632748296386266022188435706863474241774426691398717844541757205422848820464075451627099253715439955830896578187767150643891575625797207581730317734720236143970214676473559156844599870803810311912826149397160614218127292751865704184836988227164597052493438374123515086436662217591180737285001160410991209104027462930570630400578944121050594973545228077826649189156398440126207160041786874885654558558394740357297469515032108400141826917061880195730117087706726515285071357712810205577521508495357782988512641638623253960767567002215481810394236104104805979587255191262964355065104064226548570904818718928486676198546870924690209435573338222886785662111302240119887173956251865715419352476146674511560038265866613923490549631995316097273116330113994200385114686355901696827439216294162218898601178569716705140019826945668267840525214443815490015481566790370035648702971597000331244149474807564371870880417457065087376133720178930589302173054931578231426553715324361657137664012094540748117828975719340620142809702732315095581949851978461113622735098521558835482843118582842913748292411959969519919213056789621413347321785235256114939409527086005989358813644279924582386668218435598899557004491973040256945061793004708432772557327256870638423984430699526562243297162101472741888220226805951559233703382565216105029110900512845434966093328487121080009571916878830021355638877167910500096357138046387307601694412464060192456708588074666663834681230288047290452913220682039580188299003139310787586949407394767868229938664426124577683978274500307765501610296367427004774699912582544591039272356192773520109634644079503341429874757782897240818189934184856628448062914547784770335579275521074963352123348316492635450404869434947193492321053422444605782719171206005122588302376766823782832813721189080517389803728810376184302956029682659262627969974389926242793020393598532290492918560886058647376474457924462910201230107059037199337246087976987514103913012367930432966013730658877184345039415842405370587725070032802179946005503253393591052943017873268127781360121914853257246843379414934300712081967460796913257917762782063962640699330398919123540708500272319798872215111563866720681081249096461574093520499762040069288400211098663186807545408776335942500126354067144162253363289857535955711157207573636991133612457647324782857692871648546544141263072552399029448645983249196711597064242640569879401213021838267104243358560964597164438425993060901466487827744329119598593341349642748117188324642627084868633408636830258515175061999761595504356789975276765713982957672102809117022938397527591101399937583670126991511677464738377595958358919282008379160712874742773083029224812486586940087375253676269667871417372157003513944596519238062833410381033455074282223780461665662257531960733189114448410793303434005598340419623301881672139251547207494557145723543664521980521661842177047990288607171539487056552432925923520660961237187061529101972847156393289368228859516785029256959239216259732282167278818248533741006499019792563695500942871993359151930438696160168445186556793262161283198786951936088221147381700015427738343715922859391841631887855972791151545962913651430324196213189799591956727614326757608509428265317908614140142931506369342497240185824317252920824744141727623239842365559067854197519296496542020536213102731561936200983050267928023241383546175529247449018299784905797203846747837586149425954092408463433162088985525634880016317307923636757620624243591615733576681849803016876295740069395117177748667996777630583037508732746051293104656968350340977353444433368921299949224405833373871073231568351203656804050747952132144011214188909319068868676801480878272271696821566997447562969780078067449560041261263783899709522255207226730130979225251015690655076450314194205013935962422694805274366534298933074152425606790972016439623143483754510343348050718299508746555662338731792858159568740817092619092990870968997739251161788802452496206835637156038673886285678101765104502391079921647749825088936563355092067099170222522067013429043764623302502550683059811970846828580840981536704121385052122512466632654348440725223538833983992276054222392989133098635092263079048942242516726551922739451592476212183428520395839257554394609055229606889477399073728187255346084323757350610734484319511047968862242524165530131487994528719871359441580818946715688433276791495640174477394517370431398070745245339496433167224804010528527130723320032352091749202392108562007755099314650425059062157874192323224476855303689027284562385720804793740119868481587675676231057971746397386006672473285540798797039191421220650299283516116454917490601525919529887943112315479520228713439675527516679231629072668971105064332719426370456690139975652336197054022262524414428869359514976515624584777804874844107406420347038504245842199637672023375143890905837583173361671917014632329742225074637157106675945879668240669641703596017738048518705989867189508012401117926105250071431937469593234468601764488345848830020568210951173504403949066559439581281567327246537811513974881610238114062081355089025871542412021029014617288837685695225632403048698627080994468978628364802383469546227123260135580796004932553985893279256769155543782301700493752163718288898437778681507217945344097328261860336744546701232496056830093341011053869769398480440218723250366835212612971304543358371396697557928264875837249827183581758553243128630122128951700345495411549275605543458253367972721544526670297631288587829190125383254969563120054047972599081696617412794441304058271433297976224267042314404826903844110246179275727376348159066574890303555142018543031339137922943016263011820726296123706236859412362585229234564110479768919554192206433745882066568251325688269029919840978912536711561294864630058905758006012672268763230078204614028733424847375982328343260058791326242972972498121138061273508387278592639963323528155036640861441047919393934469165447074272078835113491579435870584533991270654794179390624373929610137568997135062207526423599888863008425444272677837888382843996723524508822412032523332421462612912266385790688942788216557295052181674041842586758005491216869388090910183907146690909889881386270227264480469615358211138940506276274106660156761255774656071989803405874810104365056759121750931728675821425587470055855843894538022171823029779937558005813839201722193998167431815566907732824612853685961014720086342815719770291241873427172436846753436597259733601683525689695570476043475398623035400360752982035220131056462334838579956424164469858031199429822335043760122253102498589267120027426242750061224852537429651660673911740594858866587048812528358187504269201073603046402477427924467211952544017657118389389769603354981341874055600741433602152354958926293893588232366428407470376058625530513989115262146534481130888953115798944712372935800741695043965640484434827953887910761565156145352733247513618227304635990904832211770588653249205945241648927889891864834237665304231925926163090655607441160748050560678359107856344751565440548956960326300666828447595244656688050735106157515354625766765389332907900518981860256078011082851516157799799875483048340613865048851885062865643840656992332919792132428137452247255294582580476041272676303294750672101826370036883704155227789632331594478624353255192170837702651123052966600106406508129988427692566091671618496182959723488650481766127308379809646747670462270216333248420962172577880139634475223173906626061320884817536067331451585608295722168400455736003388417006984817685581519425234674416838277217703114306220200499337120897329754800175981300864357696122890111958884045365600745859618392835325424530450837324130450505349415036868143768275803519866715440534357224139391132984013613665667862228911980561231628099465049595413672494883304370699027565826350026364279710714754679471313029535993176408893096846638077809114019306229777037835016244462176507260691234451063075497768345066908976891596550999257973893150664112280215565787129030869576702422005492867885201346609531918499193623486778635925927907361708363376570060791141194447962083744952797156716903318298027065641197756895227605050068080395109604570174905543452634307968742746223597949753200012086244153041441530946218144217249489455906636711013099180328738091161004866482304230002554366651314888201822837448280718583722385115497148943480392525817612088704060775414382707936282320614195502797422634319302099296998089068049559063904179798844923629741125514784071424425247162475715817663492808314924892788493301605772522325398269693277000507467157896868884282050769643786532857239545540451116823792584720092613137827333523480191546211201839301662622881776909828499622576540104331067589257349131610349594523161030948919707797544141128850297622197221481640447893886360553932442115719019933513963715360710217797086378396756632588753690213132001627590137158161912461974183127229633560768098219695611671395860212821663293243556805133540057386944271697634717737583115710044336920264820916210775898889412659594148767221865824800292570929772156896886112132407725472307872899668241108138081369380269086712559691026372652427714963933693037706298681955624283844723676698921983940513038950194447786062391495700982046418093996208935673774898612504985038950161816072900512688603673326014717738491592344788274562030010586288430867508288989585655173849275640310610434042421327512761414958491293050671947440724522027398603430291086639751078506488432697272446649254342932045644706832668826405715646434016092456554685360302865155715981040182131370738997673021173089574635710756489255212759804979594430543488249129768815010432299946418174682689828146252920268820193885377780051049804393476780230632750495221354999366689087986601699608000487604366988830544811652555498468854156783215175512598212819196574183217387235639969535507155452053245014096174193534889201547054121066895402884882675451526646012156575450093657320017525451866281773002936107768436892485953528266226659279602906164124120660132904300407207692879165753405986917102890680201521821811973478442287907305902004478840585409299214284978945079317362104752579285992698045437309811542001533998091532883202590482754057628086300791612031451819329223831076961637237872255197880429125726667370712504091620743664022597395019651083401919963191131759614914003696211142056361598282160696374243558615674493513148714512460164334242817951397047099661337602662245939533113215453813563392854712446525681530803536348563151682909726620320376482878789593996357476695112661081684234753684987682305486433927206763629909100943388610647784823902796623246676964847392887153518422658387908487785075678870405838443704010135259623717668680816078889282269148909236596945106877426832666671635653096308820396090275519576948859521215076501606089984558747032701015382655146286643240072267745119207514424412682721201876466652271389766557803186669847424527114771905635252021399458362573448090947990869888815731133565239286574836758069852093443592769504674364318601713283427328456825152129876046638783383205352750011891113609483201280425461214983540414897400392887616529285384443583725187718035487611019063578926784451224255916269098836771064804679724251370861796819343756133074234528185522999630995876933286109133150517028535417502281458812658631434548967849392831981854446980434189998187799364255895777769036239205957620816082359167499573991195574672873305878707116403754740550875102467609422318192489827949174307454307185397088818714508929536095547799569058605537075815179258181508249150535057195611845947141945707722896277730525524017072028954105497929860850132355192782674752510150085976278450614625854252656700511076159889673549613595258846541501853173664903038796508300791023339645975205969337616513887941035109130177632836662823394923669473581177250148485183300668161601525227862427504001300499932671693429712575477175416337588792562175406925708576546182738687702461925540331822764675614608781893741256486125805686917611208671983419901615414821765868862812267278510354657667391114693920435307692037087939844604820955539808547125426153654962782685415762865184387181375587818641541033925526153940252468060870497823418739471426018672791933978452784192033392583360978397444490673467332053755980585296612049357763414531970450022193017571342801373235600434701076513186837955595222517016134773947486999128693461623635390843772760857650453973418248280027480514159894410876205887114108934976882399336545705333901855514022755711206578252830015944460334852757956276700546664261289942691948611703487858650139781210954659115950769003161312878838370774243398511615451387400401338606106522921635120353306814374188721325629829154192670900237611701915687647584411476376010828820095753482094386264586190561602637715511416738764351247729705293300569481161684327924066648495511442655421251919840230351820974491121199936071931376354718508342921195460121004366477179803362188979402309910356496381507261818934656805747240803110945462914145731958962724079641735549847242355839710982531364484294959725364032946826350727279110436869039167128980645721603531126060446791794108267823162245290749040517213952255332026533497048420289082972196235193355372916859155377887350792403926544044586180039906479963297552392311349523249028268300117694292178686129535009998487350323708481890265328280463933474978692790676080969607133180834842585012499894096084155610313819792828711273304636153895044282001892844164544073649907000560643296314817190335516530150834605090999807984429439405069103999124465268026914477181927170653184311887554726083537220156990656726991227520867927240045468316706730059251455152221291224766162873898339908909004782923466913864100244272029549905647029372713350958811601046168928773322226544418372492149892416985211531524350607841819103734372272414251156370265301324760149618192656870924889014058323604433389243586368063417721456921007066498471247907373997809059313942373823430854176528306088584349583005193482961228929040077832047393053117124144617452915837476678500759830939384634325283324687685156629987195408746094520276744852236231324869521844281293366587033218764147922044020042137238842508235831219153834526424856338749033036465306201973151924290811762260348741062032938143712300504087419851357354104887464533313029348785319015292684165079038571758939965326701536589084565341180853834302292280418592288286966957294872787044711616912053922981072230929784165490456178592006507922486853432610579855791755004717435827438153401757358969297947413173980675879466755738598660909000666308148697413140811331514457754582013887369237555513700586557585061000821049631287453754297657000369430997921263382927429745612117608707697296841454636967753687345759648702163852666343317114179788224009493945091316760969284779199421364039502066926991444698276431817542430651466490117589979197529823514180817417121638213656188185955357007465328690329651367183127355300280511179478860484155625080986819504021778000295545790942778170725313430617755448207787860226029930500767551828789182550269230969345810750995376777870307590541579478621276340611311819842044833279164869390118606206470139252620828722733323855037144090022837988294391219357140518782019241792663735594364641788294425532349892657200465654183260984517554743157893333480875741916737187325026958107580194675217249614512098831978380423683129997487550279439582864553905889715661494301841893681374984053563041234894504655928975824088693087604001864021371106837545838876428219391554569981161492202883220420309058191224514703208312077997859734457354672922522002293997279606327906747240493971956191932411191240238514639668442469777690735649020036943327763169052972342522860736250897018614124714171156333361435422787997834914839975703608288225005621238041665627371777621795610676707656462590497788853382632262634097678058464834541721931191418845182068260649148958780401657760663204161952370743448409210362709161962972087385281602351884669177092995797603839616804738781001327460014011581979309165426759764684899876928417757606758210623860497525862145288098708259164585513019765612785441414568504479882283249100977330391700677985867531773030368467543644036849724413912533239958834223379771795353927078207512975221691104876688592560317386665173829792284482568973460761236382844050456196373462089139968226429225320166095284475314617911457528174952302852113243978563625477233578249179659880911962623142967876895448401088700179507395002412320898187431682412970316735180849317355363542789439991918527945020625704323276542960400717759403776211351621946755657036275360746093672501789443576685321044469599065860268955771310230781382912629388171394133889986681817111680587139509147033446990993083466388503611954040814818410405775301593790070163129731169093931061615358220046947477862125408530944923432315052059850259317396037492065944951058139754606823685250656593501133455144050393591643249105372286090304633949443083712470347745690364261078914617272750822390708533569546782095433010947406504845013695511421763093832891068589891119724399926465630014033081569543922675659332301402232056147670553386852532557845321617456564979353092867482019046510580120017358430923025384267869700083769723498028850782203483665008517671569156788284784062976759624328361781507529044188471458083800040900949457427758484234070399855564617186551701404196412400388876037703422252388144869135238931312456891394568710893015767022563437857985735191407528763969020661823202892516454479684812699809286546278193380067831756912861425845491707607784893449108058587050628366812308186258262935609507837472881157523215879314911809568914028517405082719195633773846344085259472029751628585054838318785233559675623714812889875783360094857514841619719540294744274584806918737033816921585927017945755839426576748991453974016345652649655974999125534130887528099662847401084351218231688331401584120127892374672612369149705404736432643298128818961720837620547955950816910777720586212549958171701050423060328489844250346674976532112316798142105860774214206418532186853631514275841104302320206117052131299995955131704711077743798551238197411888392374832133743997247838078845950448036556915758622798358487815254800069639055491125148661927518054244778390913927680431297720662983891401790594645159203948866259565183120211408949691990098690851583154590091616154865884683666235166298554966139207177730245624992321317118496877003665446549156350618011190577439509468211496173163797451118402176968523135441522199057880273559895255586518168845898061510397213157654424761693313632756586112404013644795536993189886821142876977088693582689843884749821579625346019955848640837203344096543504767344454279396231595075253286712994619385544990479298075090834204753977352585351950463096763779654610385107566603663555672219706894354809957903033973811423099301455018453871579053009943306692472372447343835965124848073982909503703148974525643398399306432315846077258657470680324970820260004288138649503975414132142163032273628857439985707811780327643868002689752580116768414413252404162800888240370178663936438471043774238495767594657132850129209165038487488516581148399093403275900053344104197691022401181012651055692006623998389634699450510816066219801818659183520392204396307152764229580510549990768426255927803997408839595142901981556813083815151545165899318332599045525358107737899507778607904035705048988621748928517023365950340932365056167774813212462131137353532342696554449271063123152779454290043283376479305106098641749401849156803445639034019850140247017222199026607012500692600721118311127241726074090577644145137801115760135504328422542033724797077976851318738742350058168029090268344111804994364024905336707471765863995457342939593494360464750127267223288799921896018194890246432561488079806024592382486836252589113783879060329880089757330248083262958615399554130846155555671346783098812713420516561951936184843670817138117104808243996424429450124768312529320519861766313553523922433757127217973351838149141456671050863962585575442684261744634136526917512932005569537835439522216318939055037394978777337509887109456477559549627081368201185484013679821511815879787149839238775581208016506552552725368479330992450464924917197849111992145750297854864531216874772915535031521154971054439450782913579749463034721228437531590201058724110325871026405717318848789443700531792769555626811360239765780089926253599693661482367184774273805448925222181443027979170672529752087616495581673401232228567266908282858049791124222168782795562447360402051199980715801375975001091673135854671514487774156125446242087046802311483894125964030239720579730743641898048360946738971782352842801207726090765944650438558092332534977020774215519690790133478978163589839951849615589708377831997613549860004344331202037868000592276538395982989744998621821905700297438933498134967433520728463727554845263995422689117942770593700035832715920818675489795661366630349934857540804481067130525889030984219558547661708497330254283098100677189692376857313052559777043023003709655066962311705888838911526249075504238252989245674165458667036109528253322800567814885568907460099721573888194880429217621339388483381629891316958464307271024609841562134020239235260271002079129687864765240814079845750217315464987557969873227655428867433041017374023351762594640827077362811303458012792292346923070887120953486554318631676659395456392586851363260932591562525482557922120459347781316691651249345273309664353725568470418480630273561139498868667351460998186824210497890041354179769743376467936679670037909530663760908210584151473815864932067889815166...

Oops. I had to shorten the result because SO gave me a warning "Body is limited to 30000 characters; you entered 319054" :-)

But it works quite fast and quite correctly !!

Marek Fiołka
  • 4,825
  • 1
  • 5
  • 20
  • Mr. Fiołka, It really works. But why does it work? Is it because we're saving each result, which are of bigz type, into a file and using each result from it, instead of saving all the values after finding the numbers? For me it was over in a matter of seconds. – VADeR Oct 31 '21 at 15:18
  • It really took a few seconds for you?!?! And you changed n = 15000? – Marek Fiołka Oct 31 '21 at 15:27
  • `test replications elapsed relative user.self sys.self 1 Marek 10 270.36 1 149.18 53.52` . It took me more than 4 mins for `n = 15000`. Sorry, I used `n = 1500` – VADeR Oct 31 '21 at 16:22
  • I was calculating for `n = 1500000`. Took me more than half an hour until I killed the terminal. Couldn't open the csv file; It was near 40 gigs. – VADeR Oct 31 '21 at 16:27
  • So how. Are you satisfied with this solution, or do you need to calculate the Fibonacci(1e+100)? If so, we can face it and try to achieve it, but the code will have to be tweaked a bit. – Marek Fiołka Oct 31 '21 at 16:36
  • I just figured out I can convert 0 and 1 in my initial vector to bigz type. `fib <- c(as.bigz(0),as.bigz(1))`. I just like doing this for no reason. The CSV file is, honestly, useless for me. But it feels good after getting a huge file. – VADeR Oct 31 '21 at 16:46