Post Reply 
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.
Find all posts by this user
Quote this message in a reply
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
Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
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
Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
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-13-2015 11:54 PM)Gerson W. Barbosa Wrote:  but only because I use a hard-coded constant

As a consequence, the Math ROM is optional, contrary to the original subject title.
Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
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}}\]

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.

.

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
 
Visit this user's website Find all posts by this user
Quote this message in a reply
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
Visit this user's website Find all posts by this user
Quote this message in a reply
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:

(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}}\]

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.

.

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.


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:

001       LBL A
002       ENTER[^]
003       [sqrt]
004       STO+ X
005       ENTER[^]
006       1/x
007       +
008       RCL L
009       # 012
010       /
011       RCL/ Z
012       -
013       RCL L
014       # 016
015       /
016       RCL/ Z
017       RCL/ Z
018       +
019       # 1/2
020       [zeta]
021       +
022       END

This was actually my first program, the HP-71B being a straightforward conversion.
Find all posts by this user
Quote this message in a reply
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! Smile

Hopefully back to stay. Valentin really knows how to concoct a challenge. Furthermore, his explanations are outstanding!

Here is an HP-41 solution:

Code:

01 LBL 'SR
02 ENTER^
03 SQRT
04 STO+ X
05 ENTER^
06 1/X
07 +
08 LASTX
09 12
10 /
11 RCL Z
12 /
13 -
14 LASTX
15 16
16 /
17 RCL Z
18 X^2
19 /
20 +
21 1.460354508
22 -
   .END.

Perhaps Valentin's formula makes for a shorter program, in case anyone wants to try it.

Best regards,

Gerson.
Find all posts by this user
Quote this message in a reply
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:

001 LBL A
002 RCL+ X
003 STO+ X
004 ENTER^
005 INC X
006 INC X
007 INC X
008 STO* Y
009 Rv
010 INC X
011 RCL L
012 sqrt
013 /
014 # 024
015 /
016 # 1/2
017 +/-
018 zeta
019 +
020 END

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:


001 LBL A
002 FILL
003 3
004 y^x
005 sqrt
006 STO+ X
007 3
008 /
009 #003
010 #004
011 RCL* T
012 /
013 RCL* Y
014 # 015
015 R^
016 x^2
017 STO/ Y
018 x<> L
019 SDL 001
020 x<> L
021 STO+ X
022 RCL+ L
023 +
024 RCL/ Y
025 1/x
026 +
027 +
028 # 1/2
029 +/-
030 zeta
031 +
032 END

079 A --> 423.352471483
100 A --> 671.462947103

Obviously, no optimization attempts here.
Find all posts by this user
Quote this message in a reply
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:

\[\sqrt{1}+\sqrt{2}+\sqrt{3}+\sqrt{4}+\sqrt{5}+\cdots+\sqrt{n}\]

.
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
 
Visit this user's website Find all posts by this user
Quote this message in a reply
04-20-2015, 04:41 PM
Post: #14
RE: One-liner mini-challenge [HP-71B]
(04-19-2015 03:32 PM)Valentin Albillo Wrote:  .
Hi, Gerson:

(04-17-2015 01:46 PM)Gerson W. Barbosa Wrote:  Perhaps this is a more interesting problem:

\[\sqrt{1}+\sqrt{2}+\sqrt{3}+\sqrt{4}+\sqrt{5}+\cdots+\sqrt{n}\]

.
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.
.


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.
Find all posts by this user
Quote this message in a reply
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:

"...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."

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
 
Visit this user's website Find all posts by this user
Quote this message in a reply
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:

"...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."

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.

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, [...]

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.

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:

00 { 138-Byte Prgm }
01>LBL "S"
02 ENTER
03 RCL+ ST X
04 LASTX
05 SQRT
06 ×
07 3
08 ÷
09 3
10 RCL× ST Y
11 RCL÷ ST Z
12 4
13 ÷
14 STO+ ST Y
15 12
16 ÷
17 RCL÷ ST Z
18 STO+ ST Y
19 80
20 ÷
21 RCL÷ ST Z
22 RCL÷ ST Z
23 STO- ST Y
24 24
25 ÷
26 5
27 ×
28 RCL÷ ST Z
29 RCL÷ ST Z
30 +
31 2.07886224977E-1
32 -
33 X<>Y
34 LN
35 -8.03012316627
36 ×
37 1.19449787182
38 RCL÷ ST T
39 -
40 9.07189622971
41 -
42 E^X
43 LN1+X
44 1
45 +
46 ÷
47 .END.

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 INPUTN@A=2*N*SQR(N)/3@B=3*A/4/N@C=B/12/N@D=C/80/N/N@E=5*D/24/N/N@S=A+B+C-D+E
2 (S-.207886224977)/(1+LOGP1(EXP(-8.03012316627*LN(N)-9.07189622971-1.19449787182/N)))

? 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!
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 5 Guest(s)