Why does BigInteger’s stackoverflowerror in Java end up as an integer in the 19635th Fibonacci sequence

I use the following code to calculate the Fibonacci sequence to any large number The code worked as expected until I asked it to call the function 4000000 times (I only did it a few hundred times before) and the computer ran for some time, and then the console in eclipse generated the exception I pasted under the code

My question is what happened here? Does my computer have no memory at all or the memory allocated to this thread? If so, why does it stop at this number?

Additional: how to calculate more numbers of Fibonacci sequence?

import java.math.BigInteger;

public class fibonacci2 {

public static void main(String[] args) {

    calculate(p,q);

}
static long i = 0;
static BigInteger p = BigInteger.valueOf(0);
static BigInteger q = BigInteger.valueOf(1);
static BigInteger temp = BigInteger.valueOf(1);

public static void calculate(BigInteger s,BigInteger t){
    while(i<4000000){       
        System.out.printf("%d\t%d\n",p,i);
        temp = p;
        p = p.add(q);
        q = temp;
        i++;
        calculate(p,q);
        }
    }

}

Exceptions:

Exception in thread "main" java.lang.StackOverflowError
    at java.util.regex.Pattern$BmpCharProperty.match(Pattern.java:3715)
    at java.util.regex.Pattern$GroupHead.match(Pattern.java:4556)
    at java.util.regex.Pattern$Branch.match(Pattern.java:4502)
    at java.util.regex.Pattern$Branch.match(Pattern.java:4500)
    at java.util.regex.Pattern$Branch.match(Pattern.java:4500)
    at java.util.regex.Pattern$BranchConn.match(Pattern.java:4466)
    at java.util.regex.Pattern$GroupTail.match(Pattern.java:4615)
    at java.util.regex.Pattern$Curly.match0(Pattern.java:4177)
    at java.util.regex.Pattern$Curly.match(Pattern.java:4132)
    at java.util.regex.Pattern$GroupHead.match(Pattern.java:4556)
    at java.util.regex.Pattern$Branch.match(Pattern.java:4502)
    at java.util.regex.Pattern$Branch.match(Pattern.java:4500)
    at java.util.regex.Pattern$BmpCharProperty.match(Pattern.java:3715)
    at java.util.regex.Pattern$start.match(Pattern.java:3408)
    at java.util.regex.Matcher.search(Matcher.java:1199)
    at java.util.regex.Matcher.find(Matcher.java:618)
    at java.util.Formatter.parse(Formatter.java:2517)
    at java.util.Formatter.format(Formatter.java:2469)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at fibonacci2.calculate(fibonacci2.java:19)
    at fibonacci2.calculate(fibonacci2.java:24)

The last number generated: 19635 – quite a lot

1326878955489024661813666359786489363862597596267841591984718441133687207625589984392446472136708362660021932584888435387175864913621007686907449488726146555339634440057581462744837894455258320181715917058010972459381316662624912515730463333990641248252946263758273263733494371408111666799248602654348374354575665103468235220997087827622833404218721 5296795469714967907029177585255931953041097074190910785453226029971398760408984833172078432454184605198390087994150395060285572150251583165471868389499945672843481685947515587743034377499766143532077192661721528003993359997246530934525456717833943628095217461954182586288476707560754969065064084782194766518458888910291084801007757831815520947787740 12075581174246291938776732162276663007926660261885810696159684398850766130466749892237961368978537350659503384887184590728029938940346029664854098332325276075803004668683973663911965166244655623288976669353392684493773256538286461459851109985316594218567036331110620166503752002858960345948009368720912 9483990019109169869972202653624366647857999434 3910947620181792632830778710190311139396156735880074125397511408966605505552422864103569572257613749591889214289715689869250327711408697220359084539413697662799047739270265811002163302937592845234417596161934244642557769201791982828989296516960843921418505825929942814213639292708769436360663239620071203876284640114617617407623573142799950934154067 0365435936962421082118957427657335120940572568809553108575779038595645790567465317987384989418130735215352197044454938184041888560239803018686185164698294191217575912300961041471253846379016838111539458503199340650847925170830041308909104620240254727212719485695991233607012305400097152820930170671679996786586356630874116104885500024255459477605145 0015602530130830632799865916436910426907366815701787618197467431739535541208421734061411825946504001994253317591124427008430838958355344794310454651478239057184460401880272193690440447468818908709573559416973460476456159514222828127654757419791409169485455 44135891590984994421316253565099062872856720500802150529267476814926424230164415350607018124 2238265060736978048559868387956160618589900694144893516842056223562659983574582770029522671424631473401781040296472536986065247377619913145937120795036202439815288145371316378406620804256354366292568284277601526223052039161567965208997051741677342969084084514529962670562971904235724951300259522336727790146934482140537946422666803487387530768149156 8655204909774495913318375555411759980733171712799322946052444470529341466911138600782570876081467456539333314140771233788511026610245435362670217650351862005123494882440312897401664665221934387408681977959790208001158782272459157216112763316198348542078709999150647564964914134863576486661123746003816160948281150545905672098416477331230622651937768 742259299137290941586718911557287342694235083529941819644830145463692894576389371194864252193944417249897576680296390036537160332193922853706900504472769773990832173851162215971909022315739548513111748800103501 077095982227131161490400275473107114332912752011610992580490036511357042388077219595501595468366432307357375880006328534616109858423036370 9170767300665124111908136212219882256188015145743813833566993241136920947969709074494693708400490165266626524438095822913197901853424142552914665703325721588414339753286621643113380168519173704794299920070190363642592959509902815769095947607009395818564394761404808083068108231669038621271175725129657162933085831476237663497229542220441135404939843 5246045511254655517277718983900530833320188511414211146698090535225453522966541012204143824248453649100578724828688310348160123621209458528951047344255833328038412893164379387055901690802972739928114327516209154924343433813058215903873087748456231395146808816246705104628538431451588404880679735578696218026459996037318513644271555799763221584367218 46089173289826579664038171218473867618979447795005708240732801965744843651457112159188844806487240388585027463071736206026836829079695983829205181777389176553032133 81000153176621747937338966968659109054686337516427263532942648787865802396994723787388617964385931023490

Solution

This has nothing to do with BigInteger, which is happening because your calculate () method is calling itself Once your call stack is deep enough, it will run out of memory and the JVM will throw stackoverflowerror

There are no termination conditions of any kind in your calculate () method Each time you enter the while loop, you start another call

In fact, it seems that you are trying to solve this problem both iteratively and recursively

You should not need both a while loop and a recursive call in your code In my opinion, if you just take out the call to calculate (), your program will work normally:

public static void calculate(BigInteger s,BigInteger t) {
    while (i < 4000000) {
        System.out.printf("%d\t%d\n",i);
        temp = p;
        p = p.add(q);
        q = temp;
        i++;
        // calculate(p,q); // you don't need this
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>