[VA] SRC#002- Almost integers and other beasties
|
02-15-2019, 04:29 PM
Post: #31
|
|||
|
|||
RE: [VA] SRC#002- Almost integers and other beasties
(02-13-2019 01:49 PM)Albert Chan Wrote: Your code might be shortened and faster by removing (-1)^k factor. Sure I am aware of these, but initially I was not interested in optimizing the code for speed, only for size. Anyway, since the summation can be evaluated without resorting to exponentiation, as you've pointed out, there's no reason to use it, at least inside the loop: enter k; sign := -2*mod(k, 2) + 1; sum := 0; a := 2^k - 1; for i = k to 1 step -1; sum := sum + sign/(a*i); sign := -sign; a := a div 2 next i; display sum The RPL code, however, is now slightly slower, because either it's not properly optimized yet or the LongFloat FY^X function is very efficient for integer arguments. %%HP: T(3)A(R)F(.); \<< RCLF SWAP -105 CF -3 CF DUP 1 + 'DIGITS' STO 10 LN * 2 LN SWAP DUP2 / LN + SWAP / CEIL 2 OVER ^ 1 - OVER 2 MOD -2 * 1 + SWAP 0 4 ROLL 1 FOR i OVER i FMULT 4 PICK FMULT FINV FADD ROT NEG ROT 2 FDIV FIP ROT -1 STEP UNROT DROP2 DUP FADD 2 FLN FDIV 12 FINV FADD FPI FSQ 6 FDIV 2 FLN FSQ FDIV FADD ZZ\<-\->F NEG SWAP \->STR DUP SIZE ROT \=/ -51 FC? { "." } { "," } IFTE UNROT { DUP TAIL SWAP HEAD } { "0" } IFTE UNROT + + DUP SIZE " " REPL " " "" SREPL DROP SWAP STOF \>> # 1A8h, 428.5 bytes 141.34 seconds on my HP-50g, for 100 (previously, 132.59 seconds) 1 -> 0.9 2 -> 1.0 3 -> 0.999 4 -> 0.9999 5 -> 0.99998 6 -> 1.00000 7 -> 0.9999997 12 -> 1.00000000000 20 -> 1.0000000000012374125 40 -> 1.000000000001237412575736110228719610646 50 -> 1.0000000000012374125757361102287196106466728742977 100 -> 1.000000000001237412575736110228719610646672874297732048196548443844171825640530428850913885586193525 Regards, Gerson. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)