One-liner mini-challenge [HP-71B]
|
04-13-2015, 08:14 PM
(This post was last modified: 04-14-2015 03:30 AM by Gerson W. Barbosa.)
Post: #1
|
|||
|
|||
One-liner mini-challenge [HP-71B]
\[\frac{1}{\sqrt{1}}+\frac{1}{\sqrt{2}}+\frac{1}{\sqrt{3}}+\frac{1} {\sqrt{4}}+\frac{1}{\sqrt{5}}+\cdots+\frac{1}{\sqrt{n}}\]
Write an HP-71B BASIC program and use it to evaluate the sum above when n = 10¹². The program must fit in one line. Accuracy in the widest range as possible is desirable, but if you manage to show 12 correct digits for that particular required n, then it is ok. I will present an 80-character long program that gives accurate results for n as low as 30, in less than 300 milliseconds. This can be extended to other HP calculators and the WP-34S as well. As a reference, a 21-step program on the latter gives 12 accurate digits when n = 30 and 33 or 34 correct digits for n around 100000 and greater. Have fun! Gerson. EDITED TO CHANGE THE SUBJECT TITLE AS THE MATH ROM IS NOT REQUIRED. |
|||
04-13-2015, 10:14 PM
Post: #2
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B wITH Math ROM]
At this point I wish I'd implemented some additional functions in the 34S. The Hurwitz zeta function or polygamma would help a lot here.
- Pauli |
|||
04-13-2015, 10:34 PM
Post: #3
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B wITH Math ROM]
Yes, Hurwitz zeta function along with the existing zeta function would allow for exact answers. I wonder if the Prime has it.
Gerson. |
|||
04-13-2015, 11:40 PM
Post: #4
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B wITH Math ROM]
And the result can also be expressed fairly simply using the polygamma or polylogarithm functions -- all of these functions are interrelated.
Your reference lists an expansion of Hurwitz zeta in terms of the Bernoulli polynomials valid for integer arguments such as here. These in turn are defined by the Bernoulli numbers which are available on the 34S. Any program taking this approach would be extremely slow I suspect. - Pauli |
|||
04-13-2015, 11:54 PM
(This post was last modified: 04-14-2015 02:41 PM by Gerson W. Barbosa.)
Post: #5
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B wITH Math ROM]
On the WP 34S I get, for instance:
34 A --> 10.28708841498052 (after 2.5 seconds, DBLOFF) Almost instantly on the HP-71B: 10.2870884151 but only because I use a hard-coded constant (no point taking about three minutes to compute it on the HP-71B for this application). Gerson. P.S.: My HP-41C displays all ten digits correctly :-) 34 XEQ SR --> 10.28708841 About 1.8 seconds, regardless of the argument, 22 steps. |
|||
04-14-2015, 01:06 AM
(This post was last modified: 04-14-2015 01:08 AM by Gerson W. Barbosa.)
Post: #6
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B] | |||
04-14-2015, 12:53 PM
(This post was last modified: 04-14-2015 12:59 PM by Gerson W. Barbosa.)
Post: #7
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B]
(04-13-2015 11:40 PM)Paul Dale Wrote: Your reference lists an expansion of Hurwitz zeta in terms of the Bernoulli polynomials valid for integer arguments such as here. In fact one of the arguments I use is not integer, but the reason I wasn't able to use the integral representation of Hurwitz zeta on the HP-71B is that it requires Re(s) to be greater than 1. Anyway, I'm using an empirical approximation I'd found prior to checking the Wikipedia article. So far I have only the first few terms of the series, but they suffice for this application. If Hurwitz zeta were available, then an exact solution would be possible: \[\zeta\left ( \frac{1}{2} \right )= \frac{1}{\sqrt{1}}+\frac{1}{\sqrt{2}}+\frac{1}{\sqrt{3}}+\frac{1}{\sqrt{4}}+ \frac{1}{\sqrt{5}}+\cdots+\frac{1}{\sqrt{n}}+\frac{1}{\sqrt{n+1}}+\frac{1}{\sqrt{n+2}}+\cdots\] \[\zeta\left ( \frac{1}{2},n+1 \right )=\frac{1}{\sqrt{n+1}}+\frac{1}{\sqrt{n+2}}+\frac{1}{\sqrt{n+3}}+\frac{1}{\sqrt{n+4}}+\cdots\] \[\therefore \frac{1}{\sqrt{1}}+\frac{1}{\sqrt{2}}+\frac{1}{\sqrt{3}}+\frac{1}{\sqrt{4}}+ \frac{1}{\sqrt{5}}+\cdots+\frac{1}{\sqrt{n}}= \zeta \left ( \frac{1}{2} \right )- \zeta \left ( \frac{1}{2},n+1 \right )\] We can use W|A to check our previous result for n = 34: zeta(1/2) - zeta(1/2, 35) Gerson. |
|||
04-14-2015, 06:55 PM
(This post was last modified: 04-14-2015 07:02 PM by Valentin Albillo.)
Post: #8
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B]
Hi, Gerson:
(04-13-2015 08:14 PM)Gerson W. Barbosa Wrote: \[\frac{1}{\sqrt{1}}+\frac{1}{\sqrt{2}}+\frac{1}{\sqrt{3}}+\frac{1} {\sqrt{4}}+\frac{1}{\sqrt{5}}+\cdots+\frac{1}{\sqrt{n}}\] . I guess this 63-char command-line expression will do, which once keyed in can be executed repeatedly by pressing [ENDLINE] and reusing it from the command stack: INPUTN@2*N^.5+1/(2*N^.5)*(1-1/(12*N)+1/(192*N^3))-1.46035450881 ? 34 10.287088415 ? 100 18.5896038248 ? 1E12 1999998.53965 ? 30 9.58513017659 ? 25 8.63931219119 ? 20 7.59525502536 ? 15 6.41399460857 ? 10 5.02099790236 If desired, it can be turned into a program by simply issuing a line number in front of it (say "1 "), which will store it as a 56-byte BASIC program executable with RUN. The running time is utterly negligible. The constant is ζ(1/2). The expression can be shortened by 6 additional characters by simply getting rid of the parentheses in the denominators (i.e.: /12/n instead of /(12*n)) but I've left it that way for clarity, and the last term may be omitted altogether for a shorter expression with somewhat reduced (but still sufficient for the challenge) accuracy. Regards. V. All My Articles & other Materials here: Valentin Albillo's HP Collection |
|||
04-14-2015, 07:04 PM
Post: #9
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B]
The Prof is back! :)
Greetings, Massimo -+×÷ ↔ left is right and right is wrong |
|||
04-14-2015, 08:45 PM
(This post was last modified: 04-15-2015 12:00 AM by Gerson W. Barbosa.)
Post: #10
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B]
(04-14-2015 06:55 PM)Valentin Albillo Wrote: Hi, Gerson: Hello Valentin, Congratulations for your nicer and even shorter solution! Your results match mine except for n=34 and n=30. Yours are better, BTW. I get 10.2870884151 and 9.58513017649, respectively. Here is my original HP-71B program: 1 INPUT N @ A=2*SQR(N) @ B=1/A @ C=B/(12*N) @ D=C/(16*N*N) @ A+B-C+D-1.46035450881 By removing two pairs of parentheses and the spaces it can shortened to 65 characters: 1 INPUTN@A=2*SQR(N)@B=1/A@C=B/12/N@D=C/16/N/N@A+B-C+D-1.46035450881 I won't copy your idea of replacing SQR with ^.5. Also, if you remove one pair of parentheses, your program will end up being shorter anyway :-) Here is the expression I've come up with: \[\sum_{k=1}^{n}\frac{1}{\sqrt{k}}\simeq \zeta\left ( \frac{1}{2} \right )+2\sqrt{n}+\frac{1}{2\sqrt{n}}-\frac{1}{24\sqrt{n^{3}}}+\frac{15}{8}\cdot \frac{1}{720\sqrt{n^{7}}}\] Since this was done experimentally and manually, it is possible that not all terms are correct. (*) Best regards, Gerson. P.S.: (*) Both series appear to be equivalent, at least for x >= 0: http://www.wolframalpha.com/input/?i=Sim...2%29%29%5D (somewhat lazy to check it by hand) WP 34s program: Code:
This was actually my first program, the HP-71B being a straightforward conversion. |
|||
04-14-2015, 08:54 PM
Post: #11
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B]
(04-14-2015 07:04 PM)Massimo Gnerucci Wrote: The Prof is back! Hopefully back to stay. Valentin really knows how to concoct a challenge. Furthermore, his explanations are outstanding! Here is an HP-41 solution: Code:
Perhaps Valentin's formula makes for a shorter program, in case anyone wants to try it. Best regards, Gerson. |
|||
04-17-2015, 01:46 PM
(This post was last modified: 04-17-2015 07:19 PM by Gerson W. Barbosa.)
Post: #12
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B]
Perhaps this is a more interesting problem:
\[\sqrt{1}+\sqrt{2}+\sqrt{3}+\sqrt{4}+\sqrt{5}+\cdots+\sqrt{n}\] And here is a simple WP 34s solution: Code:
100 A --> 671.462947108 This of course will fit in one HP-71B programming line, but I'll leave that as an exercise for the reader :-) Gerson. P.S.: Slightly more accurate: Code:
079 A --> 423.352471483 100 A --> 671.462947103 Obviously, no optimization attempts here. |
|||
04-19-2015, 03:32 PM
Post: #13
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B]
.
Hi, Gerson: (04-17-2015 01:46 PM)Gerson W. Barbosa Wrote: Perhaps this is a more interesting problem: . This 69-char HP-71B one-liner will probably do as well: INPUTN@2/3*N^1.5+N^.5/2-0.207886224977+(1-1/80/N^2+1/384/N^4)/N^.5/24 which gives 12-digit precision for arguments as low as 7 or 8 and even 6-digit precision (save for a few ulps) for arguments as low as 1 ! (1,00003...). The constant is Zeta(-1/2). Of course one or several final terms can be omitted if desired for a shorter expression with somewhat reduced accuracy. Regards. V. . All My Articles & other Materials here: Valentin Albillo's HP Collection |
|||
04-20-2015, 04:41 PM
Post: #14
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B]
(04-19-2015 03:32 PM)Valentin Albillo Wrote: . Hello Valentin, Thank you for your ongoing interest in this fascinating subject! At page 81 of his book Prime Obsession, John Derbyshire says: "...if you take the trouble to actually work out the sums and add them up, you see that the first ten terms add up to 5.020997899..., the first hundred add up to 18.589603824..., the first thousand add up to 61.801008765..., the first ten thousand to 198.544645449... and so on." The first problem was an attempt to overcome the trouble of doing these tasks which would become impracticable for very large numbers, even with the help of calculators or computer. The reading of that book has also prompted me to write these programs to compute the Riemann's zeta funcion on various RPL calculators which work for complex arguments, albeit |Im(s)| being limited to values no greater than 10, for full accuracy. The odd-numbered chapters become become heavier and heavier (at least for me) past half of the book. But I digress. Back to your very short solution, I would like to point out I get your exact result for n = 1, using this series, disregarding the last term: \[\zeta \left ( -\frac{1}{2} \right )+n\left ( \frac{2\sqrt{n}}{3}+\frac{1}{2\sqrt{n}}+\frac{1}{24\sqrt{n^{3}}}-\frac{3}{8}\cdot \frac{1}{720\sqrt{n^{7}}}+\frac{35}{8}\cdot \frac{1}{40320\sqrt{n^{11}}}-\frac{31185}{128}\cdot \frac{1}{3628800\sqrt{n^{15}}}+\cdots \right )\] By the time I posted my first WP 34s program, I had only the first three terms of the series. The sixth term doesn't help much for n = 1, but it of help as n gets larger: ---n----------------------Sum---------------------number of terms 0001-------1.000034781967089878427137719047395------------5 0001-------0.9999676432952148784271377190473951-----------6 0005-------8.382332349275680487306898205740514------------5 0005-------8.382332347354059569143016435388880------------6 actual:-----8.382332347441762038738308734446847 0700-------12359.86189681703238439605016939615------------5 0700-------12359.86189681703238439605014782688------------6 actual:-----12359.86189681703238439605014782693 My second program is flawed, that is, it's inaccurate for small arguments (I don't remember what I did). Best regards, Gerson. |
|||
04-20-2015, 09:23 PM
Post: #15
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B]
.
Hi again, Gerson: (04-20-2015 04:41 PM)Gerson W. Barbosa Wrote: Thank you for your ongoing interest in this fascinating subject! You're welcome. Thanks to you for issuing this particular one-liner mini-challenge, it's indeed a fascinating subject as you say. Quote:At page 81 of his book Prime Obsession, John Derbyshire says: I followed the link but regrettably page 81 is nowhere to be seen, it belongs in a page gap which isn't visualized so I couldn't check the reference. Quote:But I digress. Back to your very short solution, I would like to point out I get your exact result for n = 1, using this series, disregarding the last term: [...] The sixth term doesn't help much for n = 1, [...] It's an asymptotic series (and thus divergent) so using more terms improves accuracy only up to some point, adding more terms after that actually degrades the precision. This can be seen in your example for x=1, where the first five terms do help while adding yet another one (or more) does not. Regards. V. . All My Articles & other Materials here: Valentin Albillo's HP Collection |
|||
04-22-2015, 10:57 PM
(This post was last modified: 04-24-2015 04:14 PM by Gerson W. Barbosa.)
Post: #16
|
|||
|
|||
RE: One-liner mini-challenge [HP-71B]
Hello again, Valentin,
(04-20-2015 09:23 PM)Valentin Albillo Wrote:(04-20-2015 04:41 PM)Gerson W. Barbosa Wrote: At page 81 of his book Prime Obsession, John Derbyshire says: It's still working here. There are links that lead to chapters 5 and 6 in page vii (Contents), but I cannot copy them: 5 Riemann’s Zeta Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 6 The Great Fusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 The other day links to more chapters were available though. (04-20-2015 09:23 PM)Valentin Albillo Wrote:(04-20-2015 04:41 PM)Gerson W. Barbosa Wrote: But I digress. Back to your very short solution, I would like to point out I get your exact result for n = 1, using this series, disregarding the last term: [...] The sixth term doesn't help much for n = 1, [...] You are right! Although the sixth term improves the result for n = 1, albeit only slightly, the seventh term makes it equal 1.00005029507, which is slightly worse than the two previous results. Curve fitting is an option if we want more accurate results for small numbers, as you can see in the (not so small) HP-42S program. Best regards, Gerson. HP-42S program: Code:
Examples: 1 .... 1.............(.....1....) 2 .... 2.41421356235 (.....6237 ) 3 .... 4.14626436995 (.....6994 ) 4 .... 6.14626436989 (.....6994 ) 5 .... 8.38233234743 (.....4744 ) 10.... 22.4682781862 (.....1862 ) 100... 671.462947104 (.....7103 ) 1.E12. 6.66666666668E17 (....67 ) 1.E333 2.10818510678E499 (...78 ) a := 2*n*sqrt(n)/3 b := 3*a/(4*n) c := b/(12*n) d := c/(80*n^2) e := 5*d/(24*n^2) s := (a + b + c - d + e - 0.207886224977355)/(1 + ln(1 + exp(-8.03012316626854*ln(n) - 9.07189622971032 - 1.19449787181792/n))) P.S.: On the HP-71B, this hardly fits in two lines: Code:
? 1 1 ? 2 2.41421356235 ? 3 4.14626436995 ? 4 6.14626436989 ? 5 8.38233234743 ? 10 22.4682781862 ? 100 671.462947104 ? 1E12 6.66666666668E17 ? 1E333 WRN L1:Underflow WRN L2:Underflow 2.10818510678E499 P.P.S.: The constant 9.07189622971 in step 40 of the the HP-42S program and in line 2 of the HP-71B program has how been corrected. Previously the second '9' was missing: 9.0718622971. As a result, both programs now return 1 exactly for n = 1. I ought to have noticed 0.99999999882 was way off! |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)