斐波那契数列的计算

本文介绍了一种使用Python计算斐波那契数列的方法,通过位操作和数组来高效存储和更新数列。代码示例中展示了如何计算到第100000个斐波那契数,并输出结果。
摘要由CSDN通过智能技术生成
// Example program, by zxg519@sina.com
#include < iostream >
#include < string >
using namespace std;

using byte = unsigned char;

// each byte stores a digit, for example, 0,1,2,3,4,5,6,7,8,9
//    another solution is store as a byte can store!
void add(byte* ret, const byte* b1, const byte* b2, const int N)
{
    for (int i = 0; i < N; ++i)
    {
        ret[i] = b1[i] + b2[i];
    }
    // 处理aditional work
    for (int i = 0; i < N; ++i)
    {
        if (ret[i] >= 10)
        {
             ret[i + 1] ++;       // we think when i=N-1, ret[i] won't be bigger than 10
             ret[i] -= 10;
        }
     }

}

void print(const byte* data, const int N)
{
    bool start_print = false;
    for (int i = N - 1; i >= 0; --i)   // print from high digit
    {
        if (start_print)
        {
             cout << (int)(data[i]);
             continue;
        }

        if (data[i])
        {
             start_print = true;
             cout << (int)(data[i]);
        }
    }

    if (!start_print)
        cout << 0;
    cout << endl;
}

#define MAX_NUM  50000
void print_fib( const int num)
{
    byte f[3][MAX_NUM] = { { 1 }, { 1 }, { 0 } }; // low digit in [0], high digit in high-subscribe

    for (int cursor = 2; cursor < num; ++cursor)
    {
        add(f[cursor % 3], f[(cursor + 1) % 3], f[(cursor + 2) % 3], MAX_NUM);
    }

    print(f[(num-1) % 3], MAX_NUM);
}

int main()
{
    print_fib(100000);
}
==============================================
final result = 
259740693472217241661550340212759154148804853865176965847247707039525345435112736862655567728367167447546375872230744321116383994738750910309656973821883044930522876385313349213530267927895670105127657827163560807305053220024323311438398651613782723812477745377833729991621463405005466986039086275099663936640921189012527196017210506030035058689402855810367511765825136837743868493641345733883436515877542537191241050033219599133006220436303521375652542182399869084855637408017925176162939175496345855861630076281991608110983652635299544069428420657104604490380564713634603300052085227770755444679472370903097901901486043284681985796101595100185060826491923458731339915013391993236310230186417253647713626647508013398243123170343145296418179005118795731676683497990168201184990775668645684506628739248560391404760519955006628882634587718941068037009187936500173301171002831047394745625609144493282137485557386408057981302826664027035429441210491999580313187680589918651342517595991152056315533770399694103551827527491995980225750790203779810308992298499630449625581404551700025029976432219346216536621084187674542829826139823447836658158804081900330738293950008213200937471548513102722081730543226486694963098791471436292555425262404399961532697987680751064681906879211829916796440917827186856170291810221267926740136265049978496884368097525470013100457418640644829948587255174474669565187912691699324456481767332225714931496776334584662383033382023970243685947828764187578857291071013370030009422933359729277919140921280490154597626279105705524815888405177941819290521676957660874881556786012881835435429230739781015478570132843861272862017665395344499300198006295389369855007232866513171811358866135374726845854325489811371766051946169379168844253425947812631038895204795659438071530191125396484711263890071336285691015514534233294412843572209962867461194209516610023097407099655319005081586699114454426478828726428450172533204864831945789203998489382363674561822037509734856684743388724904933703163382657176072977889179891366732519062324711803728017392157239082276922807729245666275053833750069260772105936194212689203025674435653780083183063759333450235025697290651528532719436775601566603991640488256396769307929050295148869341379912517485666707471751493897903865333813953468483780861267375543838211084489765383684831825883633991731045585090566384620250146313118310874290772926221594302042915947403061018398168550669502619737615085717611994758757221298720531206079186498036159609233959410411863516885488391191851790615115627529361584900087215019222651178531508925102752804515123860379218469212153382928713692432152733271415747882959026015719548531644479454675028584023600023834479052034510803328201380388070898073483262012279526336067736698757833262548594490602191736886778624112056210983698501972901771578011204045864915393511578349954610063663574544850824188827906753135995051920622297601537652979730858816487311730823705982848940448740393205359293597645416556079547247786202996923295613897198946794221872736051233655952113310877875822887959758032045960847902450638519417431261637751045992110248687949634170686209290889306852523480569259983337751039010131661781230511457193270662916712544651215174680254819035835168897170757067786561880082203468363210181302623299602759940357999777404624495211453158837035790448329315000724617341735580556783215345434117002025856080916629419863740151456957227283692196322951118776253075340259478144820465746028848550006280693481139827601685558407954216205754355729151064153759293902288435612079264370556006236798654438246437394697247194599655579550583803482559783968277608473153025178895171863072276110363050936007426226171736305861329154402469543290461625869177463057850767493748799232918175016348406881346553437099758935360740517290941269765759329515681862474712763646883655175701835341727466260730651045119576286634992284867878059108511898565355543495876166401644758802863362970404628909706773625658430023531474946123391206863214663708784469921042754156941091224656857120471724113337848981676409692498163342117685715031167104006817530319211541561195804257065869312727621371069747222602965552461105371555453249975084327520019921430191050536299600704296329780510306665063878626815765877268374512897685079636637105938091122542883583919412115477375998130192165095214013330607098731373292651816922684506344395405672981203154639232498179378046910379342216949522910079302994923750729932506305094281390279308413447306141164335561476409310442591848136393054236937897652052645634764831827263337151211203062923388928648794920973784786188486826080464731953920084039830800880386904955741975621929392211082576639768136104449002472094834032679676883762139674407571388729286307982184931434387977808873795889684094614341592713175783651145782893558185990292353438888884658745213083813777944363611976283903689459576012031650227985790154534474735270697285145459986142290273729113146378204551622544753535677362279364854503571020864454120898423503890877022303984938021473480968743333622544915011741175157070456105089527400020638049796796040261781866448124854726963082347337724554339051984130876978127656591676422902294818176307571025579336500815228638363449313808997178508707086363220586901893837776606300606675773242727292924742129526500070664672273000995612419140913898467522495579072939849560875045669421777155110734663045660394413623588844367621527392859707228793735596672392461382746870321785845994825751474540643646099705931612059684156047323439665245723165031779283386059038836041769142873273570398680334260467007171736357309112298130690328613712259793709660577517296452826375743407579228218074435290866960685402171859789116633386385858973620911424843217864503947919542420819162608857106911043399480147301310086984886643072121676247311961819073782076658296828079607948225954903632826657800699485682530053643667482253460370513450360315215429694399186623685763806235120988444874113860017117364763212602
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值