Mardi Gras True Fibs
|
06-17-2018, 06:00 PM
(This post was last modified: 11-12-2018 01:43 AM by Gerson W. Barbosa.)
Post: #6
|
|||
|
|||
RE: Mardi Gras True Fibs
(06-14-2018 12:41 AM)Gerson W. Barbosa Wrote: \(\psi \approx \frac{1}{F_{1}}+\frac{1}{F_{2}}+\frac{1}{F_{3}}+\cdots +\frac{1}{F_{n-1}}+\frac{1}{F_{n}}+\frac{1}{F_{n-1}+\frac{s\cdot F_{1}^{2}}{F_{n+2}\cdot F_{1}+F_{n-1}\cdot F_{2}+\frac{s\cdot F_{2}^{2}}{F_{n+2}\cdot F_{2}+F_{n-1}\cdot F_{3}+\frac{s\cdot F_{3}^{2}}{F_{n+2}\cdot F_{3}+F_{n-1}\cdot F_{4}+\frac{s\cdot F_{4}^{2}}{F_{n+2}\cdot F_{4}+F_{n-1}\cdot F_{5}+\frac{\ddots }{\ddots F_{n+2}\cdot F_{n-1}+F_{n-1}\cdot F_{n}+\frac{s\cdot F_{n}^{2}}{F_{n+2}\cdot F_{n}+F_{n-1}\cdot F_{n+1} }}}}}}}\) Or, in a more compact way: \(\psi \approx \frac{1}{F_{1}}+\frac{1}{F_{2}}+\frac{1}{F_{3}}+\cdots +\frac{1}{F_{n-1}}+\frac{1}{F_{n}}+\frac{1}{G_1+\frac{s\cdot F_{1}^{2}}{G_2+\frac{s\cdot F_{2}^{2}}{G_3+\frac{s\cdot F_{3}^{2}}{G_4+\frac{s\cdot F_{4}^{2}}{G_5+\frac{\ddots }{\ddots G_n+\frac{s\cdot F_{n}^{2}}{G_{n+1}}}}}}}}\) where \(G_n\) are the terms of a Fibonacci-like sequence, such that \(G_1=F_{n-1}\) and \(G_2=F_{n-1}+F_{n+2}\). That's how I've been doing it: the two last elements of both sequences are calculated and the sums are computed starting from the last terms of the series and of the continued fraction. Now, why waiting 60 milliseconds when 30 millisecond will do for 1K digits? :-) Same as the previous method, except that two terms of the series and two terms of the continued fraction are processed at a time. OPTION ARITHMETIC DECIMAL_HIGH ! Reciprocal Fibonacci Constant DEF SQRT(x) = EXP(LOG(x)/2) DEF Fib(x) = INT((phi^x - (-phi)^(-x))/SQRT(5) + 0.01) INPUT PROMPT "Number of digits: ":f LET t = TIME LET phi = (1 + SQRT(5))/2 LET k = CEIL(SQRT(f)*(phi - 1/(5*SQRT(SQRT(SQRT(f))))) - 1/2) LET k = k + MOD(k,2) ! if k is odd then k = k + 1 LET sr = 0 LET a = Fib(k) LET b = Fib(k - 1) LET cf = 0 LET d = (Fib(k - 1) + Fib(k + 2))*Fib(k) + Fib(k - 1)^2 LET e = Fib(k - 1)*(Fib(k - 2) + Fib(k - 1) + Fib(k + 2)) FOR i = 1 TO k/2 LET sr = sr + (a + b)/(a*b) ! or sr = sr + 1/a + 1/b LET cf = b*b*(cf + d)/(a*a - e*(cf + d)) ! faster alternative to LET a = a - b ! cf = -b*b/(e - a*a/(cf + d)) LET b = b - a LET d = d - e LET e = e - d NEXT i LET cf = 1/(cf + d) LET r$ = STR$(sr + cf) LET r = TIME - t PRINT r$(1:2); FOR i = 3 TO f + 1 PRINT r$(i:i); IF MOD((i - 2),10) = 0 THEN PRINT " "; IF MOD((i - 2),50) = 0 THEN PRINT " "; END IF NEXT i IF MOD (i - 3,50) <> 0 OR f = 0 THEN PRINT PRINT "Runtime: "; PRINT USING "0.##": r; PRINT " seconds" END I may provide an RPL version later, but I will update this post rather than adding a new one. ------------------------------------------------------------------------------------------- ( 10-31-2018 12:59 PM ) Update: RPL version %%HP: T(3)A(R)F(.); \<< PUSH RAD -105 CF -3 CF DUP \v/ DUP \v/ \v/ 5 * INV NEG 5 \v/ 1 + 2 / + * 2 INV - CEIL DUP 2 MOD + DUP 0 ROT [[ 1 1 ] [ 1 0 ]] SWAP DUP2 ^ 3 GETI UNROT GET 4 ROLLD 4 ROLLD DUP + ^ 1 GETI UNROT GET 1 + 0 1 8 ROLL 2 / START PICK3 + DUP PICK3 * NEG 6 PICK SQ + / 4 PICK SQ * EXPAND ROT PICK3 - ROT OVER - ROT 6 ROLL 6 ROLL 6 ROLL + LASTARG * LASTARG 5 ROLLD 5 ROLLD / + ROT PICK3 - ROT OVER - 6 ROLL 6 ROLL 6 ROLL NEXT ROT + INV 5 ROLL + EXPAND 4 ROLLD 3 DROPN FXND PICK3 ALOG OVER - PICK3 * SWAP IQUOT + \->STR DUP HEAD -51 FC? { "." } { "," } IFTE + SWAP TAIL + 1 ROT 2 + SUB POP \>> Checksum #FF1Dh, 464.5 bytes 1000 decimal digits in 22m23.4s on the HP 50g 5000 decimal digits in 22m03.1s on the emulator (@ 2.4GHz) 3. 3598856662431775531720113029189271796889051337319684864955538153251303189966833836154162164567900872 9704534292885391330413678901710088367959135173307711907858033355033250775318759985048717977789700603 9564509215375892775265673354024033169441799293934610992626257964647651868659449710216558984360881472 6932495910794738736733785233268774997627277579468536769185419814676687429987673820969139012177220244 0520815109426493495137454166727895534447077777584780259634076907484741555791042006750152034107053352 8512979263524206226753756805576195566972084884385440798332429285136807082752266257975118864646409673 7461572387236295562053612203024635409252678424224347036310363201466298040249015578724456176000319551 9879059699420291788669491748080967465236826540869383990698732117521669570638594118145536473642687824 6292616665010009890380482335951989314615010828872639288766991714930405305774557432156116729898561772 9731395370735291966884327898022165047585028091806291002444277017460241040417786069190065037142835294 2454906015930443552368392371850498719863076339159493659614298835472459099294708267223120172721663872 1241823194246196105775201615274957788183244262017132119335542401421132222670058507790336805573465162 5058402299086387504152574693682112471825684464555601631627713455714375145500762487099156182826892640 7729981514733268896496155714492245865861324197542493067817862593585541905339717891505455586364455620 7938031405845058145545072174155386643901307237932936933245934080446768657784068880825674051153703481 6565354065630569784533262080499053683459751442356111983992647704742776980310110588155817184923932868 9438984433249056919549172687609559508041248481928325971638730802925381959976980415448551217281602262 0331943066629732759531922276659735532353604819525210286700142139979788424355360167959912204261837046 1574804015105025009991759436846692219596062731657764876794148052152436368951852744138140626600534344 1160130978507273085641037453649905644909256402247923589315487176694924367035950430223730003069311959 1523084241294295877978265000937094948239588179495651757952859542579393812959286901360210094380436689 5230969934136129044170346282641703783983574562233703887197009716007655706542390388747191135079115407 6113460540598157919023057562646715753591649187770148755458218502626932492405540852156225044719698236 6623355498628168969666836277890783224455230677866118619070146699590190480961816398991488427181733025 7799164004770479186345072685639394496425950756832920369578587382609087706169484435450991486528604325 6563839001606334030611563035396354098363218835834473080883901296586279484573619192828722289336977367 9782378754977958192808421513984809259437077374990007908490456071228874450083433934927866138800198957 3486716838942067462244279771811654370303605767679629403938379268668419000869373339749628021473634107 0780692080916455960126840415260211299965404360464933269566721032825925715351813046845293343263071155 3780174631715324612012797455054029294074788084660845192475899463976159512778843013427331220165756756 5806148633539294695176441885218053766707456569860604420895261095085491873703174092680684538011907540 1291329997487197399311819288969392181365583868351734155681678400034698319713346050391102634546299985 6564757223256093111851365655574482480889227407246960323744541055785554440720166467493389833937288915 1220506417791420513075047178464494481190580355233808384792957016490096134916888581297171817768451298 1665227044587895064381632466716623456986007517070420876709054233838759070653573007253574394206705393 5148901677468489802358168560404432836899625111068902619969865411395855004788917241289286530573960459 5295630481898478597793315200704134070112095034002798606741184490842312210024287525516602104585306029 2289223852006142473539225041071819196928056846063784532923591030025488703448802727717215706091545941 7474643895043912810893731709815790456698059914515117822228133828389982423597202827748945586513936391 3452483856497779453908416516460978752950017442754760447885185602468844734514836066921399141711901083 1690071173387674418305904927409836582097264917617521770580171687599878686607279150571455108080357714 7491050368534425788904420130557264259368697331394411507164801544674950358546377607632431556042666304 6395542580903240913570090526524804830062465306678595083246280842958974835302789009884602603688205927 5805246320877241175806919587775824030345471575187291048996858386653149338028486536933731835469634484 1504409214989225169996649489728498977896403775562848049318751954749956830451650035737932722881001804 8645762348363517623751659020950704697406203331771644770740661428929730720327030807528292464987612187 1232946637089850970178107394569897412173407761508528674095980750718116241938858864474274231665018464 7155456924995727693783282469600644529937841927359038656842258899830282105202384015914721662892536042 5063252391662328999983261110421638912207863127804805325388720787788799872365244078850903523599082782 8414181800399377391095071207605458065225083133579192674740291736305230994186482805359949788319437063 ------------------------------------------------------------------------------------------- Based primarily on the following DECIMAL BASIC version: OPTION ARITHMETIC DECIMAL_HIGH ! Reciprocal Fibonacci Constant DEF SQRT(x) = EXP(LOG(x)/2) DEF Fib(x) = CEIL((phi^x - 1)/SQRT(5)) INPUT PROMPT "Number of digits: ":f ! works for f <= 510 digits LET t = TIME LET phi = (1 + SQRT(5))/2 LET k = CEIL(SQRT(f)*(phi - 1/(5*SQRT(SQRT(SQRT(f))))) - 1/2) LET k = k + MOD(k,2) ! if k is odd then k = k + 1 LET sr = 0 ! (make sure k is even) LET a = Fib(k) LET b = Fib(k - 1) LET cf = 0 LET d = Fib(2*k + 1) ! correct results for k <= 32 LET e = Fib(2*k) + 1 ! for odd k, e = Fib(2*k) - 1 FOR i = 1 TO k/2 LET sr = sr + (a + b)/(a*b) ! or sr = sr + 1/a + 1/b LET cf = b*b*(cf + d)/(a*a - e*(cf + d)) ! faster alternative to LET a = a - b ! cf = -b*b/(e - a*a/(cf + d)) LET b = b - a LET d = d - e LET e = e - d NEXT i LET cf = 1/(cf + d) LET r$ = STR$(sr + cf) LET r = TIME - t PRINT r$(1:2); FOR i = 3 TO f + 1 PRINT r$(i:i); IF MOD((i - 2),10) = 0 THEN PRINT " "; IF MOD((i - 2),50) = 0 THEN PRINT " "; END IF NEXT i IF MOD (i - 3,50) <> 0 OR f = 0 THEN PRINT PRINT "Runtime: "; PRINT USING "0.##": r; PRINT " seconds" END ------------------------------------------------------------------------------------------- Update: Rewriting of formulae and exact expressions for n (number of terms) as function of d (number of correct significant digits). \(\psi \approx \frac{1}{F_{1}}+\frac{1}{F_{2}}+\frac{1}{F_{3}}+\cdots +\frac{1}{F_{n-1}}+\frac{1}{F_{n}}+\frac{1}{F_{n-1}+\frac{(-1)^{n+1} F_{1}^{2}}{F_{n+2}\cdot F_{1}+F_{n-1}\cdot F_{2}+\frac{(-1)^{n+1} F_{2}^{2}}{F_{n+2}\cdot F_{2}+F_{n-1}\cdot F_{3}+\frac{(-1)^{n+1} F_{3}^{2}}{F_{n+2}\cdot F_{3}+F_{n-1}\cdot F_{4}+\frac{(-1)^{n+1} F_{4}^{2}}{F_{n+2}\cdot F_{4}+F_{n-1}\cdot F_{5}+\frac{\ddots }{\ddots F_{n+2}\cdot F_{n-1}+F_{n-1}\cdot F_{n}+\frac{(-1)^{n+1} F_{n}^{2}}{F_{n+2}\cdot F_{n}+F_{n-1}\cdot F_{n+1} }}}}}}}\) or, more compactly, \(\psi \approx \frac{1}{F_{1}}+\frac{1}{F_{2}}+\frac{1}{F_{3}}+\cdots +\frac{1}{F_{n-1}}+\frac{1}{F_{n}}+\frac{1}{G_1+\frac{(-1)^{n+1} F_{1}^{2}}{G_2+\frac{(-1)^{n+1} F_{2}^{2}}{G_3+\frac{(-1)^{n+1} F_{3}^{2}}{G_4+\frac{(-1)^{n+1} F_{4}^{2}}{G_5+\frac{\ddots }{\ddots G_n+\frac{(-1)^{n+1} F_{n}^{2}}{G_{n+1}}}}}}}}\) where \(G_n\) are the terms of a Fibonacci-like sequence, such that \(G_1=F_{n-1}\) and \(G_2=F_{n-1}+F_{n+2}\). The number of terms of the series plus the number of the terms of the continued fraction required to yield d correct significant digits is given by \(n = \left \lceil \frac{1}{2}\sqrt{\frac{d\cdot \ln \left ( 100 \right ) + \ln \left ( 5 \right )}{\ln \left ( \varphi \right )}}-1 \right \rceil\) where \(\varphi = \frac{1+\sqrt{5}}{2}\) is the golden ratio. Other equivalent expressions are \(n =\left \lceil \frac{1}{2}\sqrt{\frac{d\cdot \ln \left ( 100 \right ) + \ln \left ( 5 \right )}{ \sinh^{-1}\left ( \frac{1}{2} \right ) }}-1 \right \rceil\) and \(n =\left \lceil \frac{1}{2}\sqrt{\frac{2\left ( d-1 \right ) + \log_{10} \left ( 500 \right )}{\log_{10} \left ( \varphi \right ) }}-1 \right \rceil\) RPL (HP-49G/G+/50g), 460.5 bytes, Checksum # 5904h %%HP: T(3)A(R)F(.); \<< RCLF SWAP RAD -105 CF -3 CF R\->I DUP 1 + 100 LN * 5 LN + 2 INV ASINH / \v/ 2 / CEIL DUP 2 MOD + DUP 0 ROT [[ 1 1 ] [ 1 0 ]] SWAP DUP2 ^ 3 GETI UNROT GET 4 ROLLD 4 ROLLD DUP + ^ 1 GETI UNROT GET 1 + 0 1 8 ROLL 2 / START PICK3 + DUP PICK3 * NEG 6 PICK SQ + / 4 PICK SQ * EXPAND ROT PICK3 - ROT OVER - ROT 6 ROLL 6 ROLL 6 ROLL + LASTARG * LASTARG 5 ROLLD 5 ROLLD / + ROT PICK3 - ROT OVER - 6 ROLL 6 ROLL 6 ROLL NEXT ROT + INV 5 ROLL + EXPAND 4 ROLLD 3 DROPN FXND PICK3 ALOG OVER - PICK3 * SWAP IQUOT + \->STR DUP HEAD -51 FC? { "." } { "," } IFTE + SWAP TAIL + 1 ROT 2 + SUB SWAP STOF \>> DECIMAL BASIC OPTION ARITHMETIC DECIMAL_HIGH ! Reciprocal Fibonacci Constant DEF Fib(x) = CEIL((phi^x - 1)/SQR(5)) LET phi = (1 + SQR(5))/2 INPUT PROMPT "Number of decimal places: ":n IF n < 500 THEN LET l = 0 ELSE LET l = 1 LET t = TIME ! k: # of terms of the series & cont'ed fraction LET k = CEIL(EXP(LOG((2*n + LOG10(500))/LOG10(phi))/2)/2 - l) ! k = Sqrt((2*n + log10(500))/log10(phi))/2 - 1 LET k = k + MOD(k,2) ! if k is odd then k = k + 1 LET sr = 0 ! (make sure k is even) LET a = Fib(k) LET b = Fib(k - 1) LET cf = 0 LET d = Fib(2*k + 1) LET e = Fib(2*k) + 1 ! for even k, e = Fib(2*k) + 1 FOR i = 1 TO k/2 !(for odd k, e = Fib(2*k) - 1) LET sr = sr + (a + b)/(a*b) ! or sr = sr + 1/a + 1/b LET cf = b*b*(cf + d)/(a*a - e*(cf + d)) ! faster alternative to LET a = a - b ! cf = -b*b/(e - a*a/(cf + d)) LET b = b - a LET d = d - e LET e = e - d NEXT i LET cf = 1/(cf + d) LET r$ = STR$(sr + cf) LET r = TIME - t PRINT r$(1:2); FOR i = 3 TO n + 2 PRINT r$(i:i); IF MOD((i - 2),10) = 0 THEN PRINT " "; IF MOD((i - 2),50) = 0 THEN PRINT " "; END IF NEXT i IF MOD (i - 3,50) <> 0 OR n = 0 THEN PRINT PRINT "Runtime: "; PRINT USING "0.##": r; PRINT " seconds" END Number of decimal places: 1000 3.3598856662 4317755317 2011302918 9271796889 0513373196 8486495553 8153251303 1899668338 3615416216 4567900872 9704534292 8853913304 1367890171 0088367959 1351733077 1190785803 3355033250 7753187599 8504871797 7789700603 9564509215 3758927752 6567335402 4033169441 7992939346 1099262625 7964647651 8686594497 1021655898 4360881472 6932495910 7947387367 3378523326 8774997627 2775794685 3676918541 9814676687 4299876738 2096913901 2177220244 0520815109 4264934951 3745416672 7895534447 0777775847 8025963407 6907484741 5557910420 0675015203 4107053352 8512979263 5242062267 5375680557 6195566972 0848843854 4079833242 9285136807 0827522662 5797511886 4646409673 7461572387 2362955620 5361220302 4635409252 6784242243 4703631036 3201466298 0402490155 7872445617 6000319551 9879059699 4202917886 6949174808 0967465236 8265408693 8399069873 2117521669 5706385941 1814553647 3642687824 6292616665 0100098903 8048233595 1989314615 0108288726 3928876699 1714930405 3057745574 3215611672 9898561772 9731395370 7352919668 8432789802 2165047585 0280918062 9100244427 7017460241 0404177860 6919006503 7142835296 Runtime: 0.01 seconds |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
Mardi Gras True Fibs - Gerson W. Barbosa - 03-01-2017, 12:41 AM
RE: Mardi Gras True Fibs - Don Shepherd - 03-01-2017, 01:32 AM
RE: Mardi Gras True Fibs - Gerson W. Barbosa - 03-01-2017, 04:34 AM
RE: Mardi Gras True Fibs - Gerson W. Barbosa - 03-04-2017, 03:34 PM
RE: Mardi Gras True Fibs - Gerson W. Barbosa - 06-14-2018, 12:41 AM
RE: Mardi Gras True Fibs - Gerson W. Barbosa - 06-17-2018 06:00 PM
|
User(s) browsing this thread: 1 Guest(s)