SRL Archive - Other Scripts

Ultimate Fibonacci Number Generator


Daniel

2009-09-30 15:49:36

This is the Ultimate Fibonacci Number Generator :D It isn't only limited to the range of Int64's, but the range of an AnsiString, which can support over 2 billion characters! Or the range of an Int64 (which really depends on how many loops you want it to do).

Here's the highest number i went up to:
From: Dan's The Man's Ultimate Fibonacci Number Generator
528380083589765432677493202316547207879421328870346896183434079043203799363737962347224802935034512710491126701654599379487882398944895951412901374096972891442267455898054879964586616903897771290095856541593813515554300075367030485904015852002279142755573064313348134330714716433085897157497810808547710510095405008561235866309236746637939814826308653532415410564251021544054930205428258801281786853279358532634142364252923169066887303936266186635348275992526146720626159762592569253483921979853337443299643649735112428653279809904446055597017139649588809234657725222787384665534961187916378328995866800203269637006902816415937351187403400254766616341448313730531634177910614556222413324250492499340058880242178528693028445767674184444932119333297180161900074038372687211226178709580193043354363609147626383358617285530432189730539913111823295698247680995994180077318284251788645984991879886646535175610236447407702207387401024717877012912949332619147465014582973983460114755386169607033522605940521611124112682222324125753600768508086260261523181052733355095039337247462217555506532259485869834266628017153234403107138554025638741016157075805622766631586016786095107087991991193588180276654453382923372609213720386939741904186883989232286914516682411451231652128320940407561388358199220485197514281954339286290296458526939555627542402168167577528939011941191007572861932095768354501609482964924984589738983857995009922581222413495658924470041137181447260306170241551381142463278109864482872606149326572482167771231344385571241158844347094374366387449214304613989123867939175195449143446207390595915344958661560784891533245620418017372066556152295577568494119078097262194623986175763389152196040071464892314031345960619009593658172467151967750097350358160140205434857112503278222856531371082070911299964038300189439379438072254265568865256352076017592068198901402529706716676694023397904515771226862907898560940754460783158983543832736843777378751535454977953170139266792807288407098647139681373040232358344315943344641698261491883203370620911776452356397512239609366952412840463152667788328291244797537115349909171176604643247924095436882839844656448032104021393424809561540314223465069932187521758911627844430073480911908163536295857658176854352847301448574157002720137701032308660024306743533301820489430288608279617711471778326992237818888249430135009840017170433748923784889465165934989501215740394994914161965593470027269319670950322078829473398915079007485703573166024924436308964799196223251799289214596458219465223419055068734106923414940673536103594354800682649428175630028472501137939957599808973911324024521258561650756552328615486637011259815340759988166721654941880928899938937491512059900028314696670536628733634788307054057972524641706537239376727312582146508563493381414616671972402529591574379109091255325028017023632944862866802511919278155209424593020678912204190776562685079125386070721133572684391743584976896218631755881549401280795648378392629979238839651259259224935340744518334541231370068014639363113622124041824094973137802204697157874352596703300943185751500842153648249345426096348219615038374391960181959236266982287469490354026905932031730332713619302727928325444280168271878883616656791260609408209605948252094881571897252025623606820237990538021031372895387815178899987912103136836453597351808843392480476541163288359483332327418714787148659873325195612682749129733583484851790630257107649072733594742169562026807433288993338038788211721579598518767092906927630411313421506190728616368316158627518139509305434539466771125980625555753029100566764657723530922107082760955717672169713456650555036356232609315629427285235317006474092525576730384381093930031777538450607116166846705824118371002640419882121311030392179788944069226470347248582101043188028869600164106763743973645302118307536571601251003429401415394490974277359866563472611340548513394018088874114727937568908184006155470064386048827747943581556891462327999289357562339381833625800220824106468053818669444136513602932340698316190202358412461433734660887452675879420501731407286512260563846433673704699549880205363992158161342000316291209586328477431233843221940191015129984479560427037724595432096751244753879641461347709039987562462557958086143297120534881956580425886428260704122547248410671784487736287141938265029083664303131233261240127192369822709910757847460802721810852492549915809985790911436930172535021843890613586099508388278990022925449183231498752206226425419963504162567758317861122946150720716294885150678194537346535257541891113285920315539473482768798537380664048940653530601134266372502547577539262579521364047359681710235186553556259147257529769791566363812759100036002439196696364594330680887038764506686222083195586609951246737782898919177935647881893020586525815257125420966447055196736928668967653937071446444223771732288383931593760643722531058880455355933205038943774230416005190100018272779974121206912772301584235101988473341656636143229346111369863450252108937037630717010297968467239751606570761219494642136095917094736192159767560152782333996131905028902906067240655314671236797879980297440253506981971378023053284533840663297588886955778258128772945950211648354477339397691956971862977295130783028470889538248286172269174508207182489419233580047401406522475817647802456343322673058308568930304057518139097739810400378781724496556501329245782946438731952078891681095750774959467272928779798177285510907540387095627482301031352305796161384806154341365812657659313922058086126440572551025160392513372570573086189997486053622075786113515556812208094266985055519199443187350651236708050947929304362283007495433925216719610422531130696897493171704434968774121806959308170012372276007881972093934804030127355074578448684192134811695738217222131253528984468553236939804331162495104657963241750390463859053999507302778467378538895642859938192861390510596436139521905705647522663519932347176611573252774185504010139282132614553421045387538923383066867428215393482033916055168686526982217523687640420603846044763266001510707145893889308584634829051659036214969788983802948889022002863462380453047455889107612548442908729523829207782349846734788655643423007818053354165076276807363073609281077218769863950603933315732514899696384212276417558093399095899958301358707129535904372007401635306089822476039146359471268432570424211715405924644446638850214718569327295794428999325396932620014795154065406779767874037871315622510317958982649350612523528215448998044186240321965407215969783951363663927770275303853997060275802575070655488161311884778631464748529652273221590450201145659960037489539722544661403229778620199648508707065593063760615420461368073136871305076231058552183393620850806688402407523076889096254650796326730284181878196378072883485380351064188273895797846095089952356304518465860784603454222512402474692911952203385276460601555492781448993218135273256756730118329030159087373823729423173191425643085861035058696393296846234428977586974691392710296073154421606195343675816670566156181324180357614495836353265288243715069542273884687801912986783630801101474742613845345852285779456211973092196345100831609912263817484555957954714348510005750543477254434682201226466878447941283352040417428732379054124373766219462149401377822795493038789290414011946019965251056358677351448749466844272294105087968421919080112024642644435860636353940516975186574057207431267539108511933663399836915516113002140475225252598388688910074548047373948071433547903452850992462372899791561761406770539874970204208774822461853215782805394227844251170650213018370094708524355549889322015665385363076938141793707194700235374363142054119634514879895439053050977968685306673883756443999176658232967257368953624594197529150747252997718643585518749854035133753040088122636480494122444827631522992046153415828138598906847585184602828386029751773899810448071531822012596433245660554660285881705813603375603835099384821934790743129644353882410836809625030138821781915099285008161820593086848028324944918581644851882599700477875035909642508553038307031936472649689196558439363967909499906533558426546807595048900841454220534573027941803478828592163777753879747039834281557235351774099233742693890925685135721378468416394282535396718081261424025605954888275778721559283665


Enjoy guys :D


// -- Dan's The Man's ULTIMATE Fibonacci Number Generator!

const
Times = 10000000000; //How many times to do the Fibonacci?
WPeri = 2; //Wait period between Fibonacci prints?
PrAll = True; //Print all the Fibonacci numbers?

function Plus2Str(Str1, Str2: AnsiString): AnsiString;
var
i, bI, Remainder, cI: Integer;
bStr1, bStr2: AnsiString;
begin
bI := Max(Length(Str1), Length(Str2)) + 1;
bStr1 := PadZ(Str1, bI);
bStr2 := PadZ(Str2, bI);
//bI := Length(bStr1);
SetLength(Result, bI);
for i := bI downto 1 do
begin
cI := StrToInt(bStr1[i]) + StrToInt(bStr2[i]) + Remainder;
if(cI > 9) then
begin
Remainder := 1;
cI := cI - 10;
end else
Remainder := 0;
Result[i] := IntToStr(cI)[1];
end;
if(Result[1] = '0') then
Delete(Result, 1, 1);
end;

var
D, A, Tmp: AnsiString;
I: Int64;

begin
ClearDebug; //Comment out to not clear the debug box =P
D := '0';
A := '1';
for i := 1 to Times do
begin
Tmp := D;
D := Plus2Str(Tmp, A);
A := Tmp;
if(PrAll) then
begin
Writeln(D);
Sleep(WPeri);
end;
end;
if(not(PrAll)) then
Writeln(D);
end.

Sandstorm

2009-09-30 17:34:12

Very cool, how long did it take you to return that string? I'm probably going to leave it overnight and try to max out the string :P.

Edit:

Damn, it made a second line in SCARs debug box.

122387728599817179268911076680359223892835241971036269770425818633421156531315656409535675957777695976306770097808644528073882695765594150397535764438100007800962114848471183590705035490175537163473023806201843659111575259332045227612570090129238027582669840510154680947854705360876353800202616199548838484464006881362436011482468254249763279146562938194566105649307360780972449603084257482927934299287833069477415013546835578904201410782523912807160072949644368410598378622539953168249061627938348575950394835961031528161624798256197730395362323145469867569444980962412765247742358324677261253331773638645332481806586229148198193462009180047387217198409640436184250014731922757416866270248281080607684005306814965744153675889085137111537067113724454793669493818853138176139585355080208632641415412375872011026671736184791713294431788341108514149355828510426134508389110096299227814020163634243741379001086346462090593447798290564108131286599007497798441299115349312576812541167312770838177294109781296137820990772795231203462298227429122291257699886741805037997426608421523447764858880866531415455725101438161730211938269951900555613767048301743307405649854083739709756320741574022484770865908262335253000778999189958474019608609172164080675092139784592013869946278685160577367911567503205297511133373680545564156726622818344047726418270282924775646380370911684036478657428852328172382940992742631168092708199220857242013333862913624308038123154830770581861503759166448897562486074720165940033836877367946200219399924603059278300518991434799636533770277028418034643824941151025499093550846618850785294323683050621847929047601861919790978767868314214606751646424002626258936550748894053911721322714201110177852552974476637695329985848307155571209610838517384406178091784599228122227599308613047357368582563537970578081663175054403658134707108190613872496989543935988953011519706704927042124706302688325476701378990261405151874671614093668612134473561502279099292716545867390625362502564799383583206565103467902683982907155579328963173709410265644463451928278840352098621584718551077164830914919374456396393568373046806900624679916171839847102111870024751214653616744084975071431798206484322807874084144175435751471244793446850554656620769972296897909709542793540833111681061846163636354206580565353900979555307593189739970283291535062283979009985189905682577202589062571277866419126087293575827741552663223863354637397096124230830027642975111530851959494157597971768261172892066830239868600444695550369538514629415303158972250783986549060383657121310537697525906187080525978282024666585991997298860015928930077668517731530493530282678903153457769262425518844239086455235545310894468536929954567881630124118759133886695209937310788905510469481921195917780496853933338117486920898530047916313605651494502860893011674506320047947403211062534975871740748279836975171462030596500954932907919182523098510165247042304757844034610253809845605954717085219107138535984915167242943481639872959839340372729427513916583720278166190279945420631498375120933673098906070498118551124516900894330181295589322857957394825062169


~Sandstorm

nielsie95

2009-10-01 01:04:17

Pretty cool :) But why use strings?

I made something for big numbers some time ago and adjusted it to output the Fibonacci row:


program New;

const
Lo = 1;
Hi = 1000;
type
TBigNum = array[Lo..Hi] of Byte;
var
APow: array[0..10] of Integer;

procedure InitPow;
var
i: Integer;
begin
for i := 0 to 10 do
APow[i] := Round(Pow(10, i));
end;

procedure ResetBigNum(var BigNum: TBigNum);
var
i: Integer;
begin
for i := Lo to Hi do
BigNum[i] := 0;
end;

function CopyBigNum(BigNum: TBigNum): TBigNum;
var
i: Integer;
begin
for i := Lo to Hi do
Result[i] := BigNum[i];
end;

function IntToBigNum(Int: Integer): TBigNum;
var
i, c: Integer;
begin
ResetBigNum(Result);
for i := High(APow) downto 0 do
if (Int >= APow[i]) then
begin
c := Floor(Int / Extended(APow[i]));
Result[Hi - i] := c;
Int := Int mod APow[i];
end;
end;

function BigNumToString(Num: TBigNum): string;
var
i: Integer;
Start: Boolean;
begin
Result := '';
for i := Lo to Hi do
begin
if (not Start) then
Start := (Num[i] > 0);
if Start then
Result := Result + IntToStr(Num[i]);
end;
if (not Start) then
Result := '0';
end;

procedure PrintBigNum(Num: TBigNum);
begin
WriteLn(BigNumToString(Num));
end;

procedure SetBigIndex(var Base: TBigNum; Val: Integer; Index: Integer);
var
i: Integer;
begin
if (Val >= 0) and (Val < 10) then
begin
Base[Index] := Val;
Exit;
end;

for i := High(APow) downto 1 do
if (Val >= APow[i]) then
begin
if ((Index - i) >= Lo) then
SetBigIndex(Base, Base[Index - i] + (Val div APow[i]), Index - i);
Val := (Val mod APow[i]);
end
else if (Val <= -APow[i - 1]) then
begin
if ((Index - i) >= Lo) then
begin
SetBigIndex(Base, Base[Index - i] - Max((Val div APow[i]), 1), Index - i);
end;
Val := (Val + 10) mod APow[i];
end;
Base[Index] := iAbs(Val);
end;

procedure BigAddIntAt(var Base: TBigNum; Add, Index: Integer);
var
bAdd: TBigNum;
i: Integer;
begin
bAdd := IntToBigNum(iAbs(Add));
for i := High(APow) downto 0 do
if (Add > 0) then
SetBigIndex(Base, Integer(Base[Index - i]) + Integer(bAdd[Hi - i]), Index - i)
else
SetBigIndex(Base, Integer(Base[Index - i]) - Integer(bAdd[Hi - i]), Index - i);
end;

procedure BigAdd(var Base: TBigNum; Add: TBigNum);
var
i: Integer;
begin
for i := Lo to Hi do
SetBigIndex(Base, Integer(Base[i]) + Integer(Add[i]), i);
end;

procedure BigSubtract(var Base: TBigNum; Subtract: TBigNum);
var
i: Integer;
begin
for i := Lo to Hi do
SetBigIndex(Base, Integer(Base[i]) - Integer(Subtract[i]), i);
end;

procedure BigMulitply(var Base: TBigNum; n: Integer);
var
i: Integer;
begin
for i := Lo to Hi do
SetBigIndex(Base, Integer(Base[i]) * n, i);
end;

function BigDivMod(Num: TBigNum; n: Integer; out AMod, AModIndex: Integer): TBigNum;
var
i, ii,
b, Index,
c: Integer;
Base: TBigNum;
begin
ResetBignum(Result);
Base := CopyBigNum(Num);
AMod := 0;
AModIndex := Lo;

if (n < 1) then
begin
WriteLn('Can''t divide by zero!');
Exit;
end;

for i := Lo to Hi do
if (Base[i] > 0) then
begin
b := 0;
ii := 0;
while (b < n) and (ii + i <= Hi) do
begin
b := (b * 10) + Base[ii + i];
Inc(ii);
end;
if (b > n) then
begin
c := Floor(Extended(b) / n);
Index := i + Length(IntToStr(b)) - 1;
if (c > 0) then
begin
BigAddIntAt(Result, c, Index);
BigAddIntAt(Base, -(c * n), Index);
end;
//Continue;
end
else
begin
AMod := b;
AModIndex := i + Length(IntToStr(b)) - 1;
end;
end;
end;

function BigDivide(Num: TBigNum; n: Integer): TBigNum;
var
AMod, AModIndex: Integer;
begin
Result := BigDivMod(Num, n, AMod, AModIndex);
if (AMod > 0) then
BigAddIntAt(Result, Round(Extended(AMod) / n), AModIndex);
end;

function BigMod(Num: TBigNum; n: Integer): Integer;
var
i: Integer;
begin
BigDivMod(Num, n, Result, i);
end;

function BigPower(b, g: Integer): TBigNum;
var
i: Integer;
begin
Result := IntToBigNum(1);
for i := 1 to g do
BigMulitply(Result, b);
end;

function BigFactorial(n: Integer): TBigNum;
var
i: Integer;
begin
Result := IntToBigNum(1);
for i := 2 to n do
BigMulitply(Result, i);
end;

function BigSum(Num: TBigNum): Integer;
var
i: Integer;
begin
Result := 0;
for i := Lo to Hi do
Result := Result + Num[i];
end;

var
F1, F2, R: TBigNum;
s: string;
begin
ClearDebug;
InitPow;

F1 := IntToBigNum(0);
F2 := IntToBigNum(1);
ResetBigNum(R);

while (Length(s) < Hi) do
begin
R := F1;
BigAdd(R, F2);

Swap(R, F2);
Swap(R, F1);

s := BigNumToString(R);
WriteLn(s);
end;
end.

I think I would've done it differently now, but it works anyway :)

Capricorn

2009-10-01 01:26:52

I cannot even fathom how long it took you guys to compute those numbers in SCAR, lol.

Sandstorm

2009-10-01 01:34:35

I've got it almost a twelfth of the way to a second full line in the debug box, and it's been roughly twenty minutes.

Edit:

Had to stop it so I can bot.

661403733504470426026208415041021483819238277560448665151590905570350771785787957988691526257986285435722207253940765023565311160973771120146257557771122381314871999765551630285662105538729824936224902045275235844985063140621715567025848568381369698078792611482501530887939947659728747915265559210468114629349410896486815378470024306931713327293987956563041719677794376806262889499783028105771405652095688802552322636738262690466996623863132148984760168534484120003203121294907412973627689148702132592947404744612995154079007990439443190813766348444720111898827783188831633252554451665287378450046486454685816821940521518840446710915641633802899888702485644348414437301370181949453847637100918142982991947927622199927587121045510695296470865847657035018275000971153490336901677571192785089584557920438979577919681485263327091275799098341986628326458752476035920855287607164350907264634307759095504013231572441870189001222299628255409789809657851610492063906320031285715698363787244071309776862779790668829351852007411147721402039745754091549212936651055364809153359077147343349585175120319984487903711644761882528593559175005982604764250311423014697245849816954639671390157302936418229273083601986001123034758357062661186506365938982257443604691750860407780891550615382558186628519357958070089415080322657711658042750711033233872978416624912379768500318937831969320535827825513809890657020794506956075771843588339013671016062544303121651515767066761528569328820743456989128110214291108456200157362969886084856273431145530656138521981671296695897287220610424358455754578622971732362197066325811856536749850122896408025194441544379709915474721994418246334149518926532701294616610887023794856644638014152471765646432814805198610687533748868891867734096222898627841047164628678810362100553471027139689295083746821926743167298914670646151980697780537014407825886911968420797169453883794901716898141828545679733128299546894862709820369036468944813326513199172834355634451147923690471875884038547822123508994202644529230796093630920439287440443026963015534422424897472841431141234716813007374261714246344880928492848482434352647991737680447904874163202974149345565414864327074478913513970035495989587877398178388928089925919420078770115687395154202628582083275710783666315458078977295373944457441950910469132436389894669648286774307462156326499386696831564841233176774221205952295249073137161495203696388736671443857074605953828166677882984149009034276524493633229929196995255175870273542043907187072010127971711670745223763438045716934064203799039790966879463309351522101432468433181870231875340435359715584071614954175174416323171699955551947855706590407374577229549245368285880387025811085888836936007923740275510016729318290655625781523806688301294205741622575593598593841477695179638382889425428104834121844774959312416562279648148575279088208370627202398097325302919648616217728668542206765652015290184375844941818160078552199630610390419288104221476404738441237316570593856808598936646904898904956158346286909007089682039023599939733723872356582525093053187909330353060025360147142285782560743073565123020343818144796085818184617998274792976865820775038276127748561737287795482338397789768057599308956186660955177073045133467860314788165838729854283347222642830003622037352098237322909308761224774524649740013922627105203740684582736416181443838819758952664874776011326241357168842313947573738353941714391126815175287764763127563779037046649057390545936977185328718105037577610914990604571059485691010620278783143246374274246166526431120272996169624204461793388557379816880460097816112615132854133972290522100139914029502665810072121647945408892869469688413595094790698259630884353730218266198308627693218571437019512097920022585870865415062290058885182325792920813086006909882831434540478842253625008382099175251050941989148547149004053177981259868013545798605786751315570333059329532866999839124448947488690810057466516497448179296030628322469530168411179861657217558448120866607990211014832027405511630701920508766011551587119976141321012262181694211271037046404973694787282934250214173757209532509549423292236940010975503987513658239095895579649017397010690410798890674401199233994279542398848478992290689337599547807140849882157887323787063847654304904856031503936405136374856059869791072511381657678766467516285673068743043162284625372361230488024625315643377028380850387739223329399305464241361073041924170559095856323299015015698722866258510467133930700960906341330545690798834756746208321785114184461912917856404596938595572098855359164687152167768146125784461595966247536563333134209459131885450443230145153101437077432560895794441205395944811202256977975198794102652279997357903997027232237918023311626622357718968857273100368120262060917161


:D.

~Sandstorm

Daniel

2009-10-01 11:30:34

From: nielsie95
Pretty cool :) But why use strings?

Well, because it can support a massive amount of characters and so far it's faster than your example (not much about speed, but meh) :p