1

My Fibonacci Number Sequence Generator is something like below:

import java.math.BigInteger;

public class FibonacciGerenrate {
    public static void FibonacciGenSerial(int n) {
        BigInteger bgIntX = BigInteger.valueOf(0L);
        BigInteger bgIntY = BigInteger.valueOf(1L);
        BigInteger bgSwap = BigInteger.valueOf(0L);
        BigInteger bgSwap2 = BigInteger.valueOf(0L);
        for (long i = 0; i < n; ++i) {
            if (i == 0) {
                System.out.println(bgIntX);
            }
            if (i == 1) {
                System.out.println(bgIntY);
            }
            if (i > 1) {
                bgSwap = bgIntY;
                //bgSwap2 = bgIntX;
                bgSwap2 = bgSwap.add(bgIntX);
                System.out.println(bgSwap2);
                bgIntX = bgIntY;
                bgIntY = bgSwap2;
            }
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        FibonacciGenSerial(50000);
    }
}

But after two minutes the result of my 50000th Fibonacci number is given below. And it doesn't match with the output result of the following website. Why is that? Am I wrong? How can I fix it?

666100648565481205010951547560750933431950479937080994773751415475329647357449820574055869101354533179842571730518504252690058309030165957793611893184804604322634950697308315493285225827521968081167628872452768615738108939224876141123321314473270269465000697651877688322772577296187089266717271846243032342710169463007536900896948726682347470396722227545422828097990563697789828879621460065715309733855859315802250715487615816509617290450469997736430690473288367991638342872414940097748223932840620504428668520921655344430301224215692897710294618083941298394708009681899419228230959458929147242702905587860930816067641195022945573432258310501457406158453985387540996675518570815051459141923719868534748290307729399974816799507877982414057300447384236578708136869220018082040204979278639844104757994501020243076351266748128805893493483388168490769849952216805657010874821090514296406355110553853620293655313964806869929662365912232407486790482290214419052737478156102371122601126269716933969142851424928376050243479723763826825290719288650668925541910835478719090753282030915936853660001042090112056551037656921633144893206490532744723980391060344012105109041485463735778178695972346419842041360816069653760816717602148456722833643023802496324551697060976893855597905167037325153206289367148296194735202448911826351569038642836373273388438749220506838097571642903225609197374346730325128298541730100868662836408370652884683285634505652640553978383638747687616980404728716061328452648302998982054812903248737902508504748147966013614486993459268653853968661755091110112836831904518162502479003093926076241103832746864779503324612870238425655535310753031768424140386727074621268211011923361554582637672555763296766763854351892413797277286060361458119987282967951684241873325465716615261286317039991891544731713205149083895650120764948564143504675774742730781931928449575390256866738150275504189195079536502592392448642504625645222976625257159399314999623324197626795103321718324580761387037914189285639982327419211417271975071509612777714938378809871158377357033535038841919249248086467043409693138137112595014692834270711500010138506524349690071946393449648099208714463687090366864945411336246942663509789781079889019967268789357869074643331574973686481450223653389822460520183558105963223746605217997222387889751383509988082580944620464366163610679825916386927892295095374801191974096436340267734433730251299313389962413993201863403083841538686348160621073767578057926062012975065413640006600693103780549028905864574886005678966392854693541230730899811945995142389153270240354100999411437018828868859400252967613211726276446551737789380742131931227839657181988118865833480816151389108105528537216403964625087028986820520228715010685007591856614514778740893057418957567038483687071966812519841489146071253869889423050709567596610799719867036770761423744800930144446719985845639071948611549514167774740990909139060327587087707067165105903518512015008461478015071165612693027121166154673444805231514777642042786026879557204719340331591136844403791499153922402086325805819134623464621695959806096503918813601014444481497906000448189658304143531211623553079844917054983082998776950836009095453318030659296363742980120743822658798934449668379206415174607506093762182254110615681569717083211494114018772250097676725941157779174932734820568414226363304237968436842248075823282963543871105638202916945277814768864354916760621028779029538705578282925816579093772231288716013708194434438394149151491195572603314854012840149606398382269625517147771137052311883362797842053344005270594854159706295373797302632356629421276081517886019790032658026956610798949752129107805929458801027179475571809611111740758763373153476180800487503600606253740331201651721614058623392972202709529281690876761917422759364320015857427391270406331078878439305356665324344974924966088691546853401573510776119213632887236814855337852815095530230952927329556374671331803148352243251263283265128614261836045147294319819688471388326969417579904757464922708053628945150223335578551759081194695982876790011669825263518704133543095719051298714606579941128392101513434827920391242277081025094359551288317988893709534781978393945117097207076724791494616138837298981254940507843176701027248285993023860493750030475545967637215092330757914527051682535238578874169416419343826813276679317873998472471749686675765314934017559446883914428510562722864433439498146495120119913901346238077941728002849108235739773630267418055647587363776504486028675431129509545059403089455946519091959257301580675673458296214369105815004711115705568732078472962138306788772718702122766701488203167194502460046797772368844117678537000296384367003298371856809743914987011520470356450353074991325681614617984049912889619058746341502133349510556965392824027344424606453707377080729843402550151444160443240205434479868981609992585944572035786195485899532860763384337256250361518098480850231197439501006641686919198156639962949704149039218332928999679805750001308229279436709963263406239794882894065682638778935094093844765547146298281525989658002832701160413802748514986615725425904543562884354377218482717540397490074759993076664214614879978262310338197341128247873923938475147973518208725384873960246416763890489102999975017553478512459882618858334522285043766936484250110365909823001346728831403112210757004910897939860331633776262670716334868107386964506795963587304507408784081379727762610979152292320358478558706063821848616759228308451800969357027404759064318842378084092315173807524269604159146455211809665956036125198199064555802995538741179855329046922085858818028719242720320756267564991603927439237662286873972153269427651011235776687764275443675406561338563010027840718479862348591917953933994821284372280834889531645178403889948454266146190305939429767502629047826504253391658577635431374402785042293344010121069723509203600429348621981859733004535580888240359962243091362619987113834608216353140281542703151377407446724934788657087070356643740959095887668655129378518257991750140164754529691806673352241155899083959191871012780666725664381728302763413740883234510412767276595568556388063610723825329062674666971943247295036113072723265285584887408747520460330374437559798490506055045514064213832024627976351774570181205497658698375716324872741302409695277078424193438335439292377835057730034036145478052900402863148060495085323281842554651451036178213074357384018808581022331092529011187533277675824202856654414408717211908307376739819453635469072101848013176903862214060551709095000905074280971252452952909536451044791498647604893557873228056987657309671604147796665461589224998202540292423728222584648338588736664143022108815460974616828866988926345892872995571292422421773913479426380919060101391760420261420730453554287085513644899186880442361412631217758879550052882439598889457830856779410842513718768766091275135456380178178300617147751177790796952021354316582389053446353859076939551592384382774428962913131759132408131282868099485912859413029256311254587488212460831011124533510559924989863658143059250059774988707261404862833412102527976188330569397378668933475880809400830766736994040555356783641599760677810636466319728397018292100355127463837362740187333750034023892811300477151403790798603023100128610555550613790049616753892668808336749283366803306386255139739200520795547147713447959609503199036667286329077255924746918923652728312207868221104322516466728395553078246174620007843839145562059618308045132403907998960823112902275962909973027107655355753904048396067828089331591269404084934464120850296853594525628909527716103596016612755534356543244626443354995929675356741004157822959814523198218428406546177374802501921215250213079420934002604758784198250768562575771064769772000368187985559402552864559144184644579246642229835234324930558261825479606065297775518810717430372785680588824256604595020620542104279192252958742341699434491865985473991665889763758076619448944029251378170452192928104719929641595346467381297599346164809015557379482897429306632382718572359080200580125769912394268937425208471739498198294039857540747406725444057946732876949278204480286629256472353068058903699307994090916485647143450897246288120405624044272974581328543171842457960024273937994430303017416394043067083128885809213554212555454076560792542180784246519970324808557913497445167628369709876846332995607902679917364426349371775777894415979568094641873322943199022719563252749184400889845604530664204285868733579775816968832666680229105313367395422727986751689021732800376241526129309622574934865789670249661111729875503959405546498195859589055079973719910765291871059200578848376091382450827936346881093595641008636086660186437476419800866837924558581042971863841046902667299007595753977851094063304503639295603862415053601506791945752875688662852884259197089038836135664288165187408459281783504155854777313151613458774746417419908057567542628145693509766340480961263449080763343524370041474279899270297150716040301621228556313386834331553726237194024539232248304826810025215970632165794694360001925915558517317086796919038131136526735629682618234254690009699824548131542889727269915091378373526676061198785310756633059057477359341351019021807301233526081109778738179038665569950748548783317916516531990441083102618945701517010622590869651328033237579383625761930852080497002516434690134150677105528818731097339202012653039585356617091832439042951712221740827068604212723039524324615603136864188680891174417641489121494660755507003886549855291095862457104577939327455945605614519889510432311246894620989335653836679059538065437524829322117732552766511970700736821870037554214173785971048690499128614852714423373079479669815004079790437478405606774318669283764771464300583751653017433237361104801425733258035970901068212527401383741872107655688495567909246297629646550462643273447770343300210223279668754011716842718361977583092506467739884269913691250449405207875729401675856977466707283966223128881659500958944593836216397235863285323613591565012073847795737172930233933594485324619433561564931911914888793630226393645387832882895644605397261496725355227817783507292619445421611950535223331719341500756971803362136404519452880893229885903007294748984670394618952806821098740393146788700124820764553041872496073332651308037095119882100202710186702753433978399149908454238724269918322749507909772992037226532802201150761626111638906545107602875995114792979876216193958825864398145879704552311475748837637501

But this Fibonacci generator website outputs a different result: 50000th Fibonacci number by this website

  • Does this answer your question? [Fibonacci calculator with BigIntegers](https://stackoverflow.com/questions/29617340/fibonacci-calculator-with-bigintegers) – Harshal Parekh May 13 '20 at 17:59
  • 1
    Is it a simple off-by-one error? Have you checked the first 20(?) numbers to see if the algorithm is correct? – Progman May 13 '20 at 18:01
  • Does your algorithm show that the 5th Fibonacci number is 5? – David Conrad May 13 '20 at 18:03
  • Your program shows the first Fibonacci number as 0, which is wrong. – David Conrad May 13 '20 at 18:05
  • First of all I am not using recursion. So sorry that posting doesn't answer my question. Extremely sorry. Second it's classical Fibonacci sequence which starts from 0 then 1.... –  May 13 '20 at 18:14
  • @mvr950 According to [Wikipedia](https://en.wikipedia.org/wiki/Fibonacci_number), Fibonacci(0) is 0, Fibonacci(1) is 1, Fibonacci(2) is 1, etc. So the Fibonacci(50000) is the one starting with 10777734893, and the one you found is Fibonacci(49999). Therefore it's your definition that's incorrect. – Olivier Grégoire May 13 '20 at 18:26
  • @OlivierGrégoire Thank you for clearing that up. Now I understand. –  May 13 '20 at 18:38

3 Answers3

1

Here we go:

public class FibonacciGerenrate {
    public static void FibonacciGenSerial(int n) {
        BigInteger bgIntX = BigInteger.valueOf(1L);
        BigInteger bgIntY = BigInteger.valueOf(1L);
        BigInteger result = BigInteger.valueOf(0L);
        for (long i = 0; i < n; i++) {
            if (i == 1) {
                System.out.println(bgIntX);
            }
            if (i == 2) {
                System.out.println(bgIntY);
            }
            if (i > 2) {
                result = bgIntY;
                result = result.add(bgIntX);
                System.out.println("result: " + result);
                bgIntX = bgIntY;
                bgIntY = result;
            }
        }
    }
    public static void main(String[] args) {
        FibonacciGenSerial(50000);
    }

as the others already stated Fibonacci's sequence start from 1 not 0 . So basically, with your code you are getting the 49'999th Fibonacci's number. I also improved your code to be a bit faster. No need for 2 swap fields.

Zartof
  • 185
  • 1
  • 12
0

Your number matches the 49999th number of the Fibonacci generator website you linked. So i guess you have to do a little change in your for loop and add 1 iteration:

Change for(long i = 0; i < n;++i) { to for(long i = 0; i <= n;++i) {

sapisch
  • 535
  • 3
  • 14
0

You can avoid the 1 and 2 conditionals by simply printing out fib from the start, and then doing the swap.

Or you can just print 0 and 1 and then start the loop at i = 2, ensuring that you update the running series appropriately.

BigInteger start = BigInteger.ZERO;
BigInteger next = BigInteger.ONE;
BigInteger fib = start;
for (int i = 0; i < 10; i++) {
    System.out.println(fib);
    fib = start.add(next);
    next = start;
    start = fib;
}

Prints

0
1
1
2
3
5
8
13
21
34

WJS
  • 36,363
  • 4
  • 24
  • 39