A fast Bernoulli Number method for the HP Prime  Printable Version + HP Forums (https://archived.hpcalc.org/museumforum) + Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum1.html) + Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum2.html) + Thread: A fast Bernoulli Number method for the HP Prime (/thread256591.html) 
A fast Bernoulli Number method for the HP Prime  Namir  11212013 During HHC2013 I mentioned at the end of my presentation that the HP Prime had a wonderful collection of functions but lacked functions like the Bessel functions, Bernoulli numbers, and Bernoulli polynomial. I commented that calculating the latter two was not easy, and Cyril affirmed my observation. Well I have been tinkering with calculating the Bernoulli numbers and Bernoulli polynomials. The Bernoulli numbers have the following features: 1. Aside from Bern(1), all Bernoulli numbers for odd values are zero. 2. The Bernoulli numbers for even values are not zero and alternate in signs. 3. Bernoulli numbers start as having small values that increase to very large values. I suspect that this variation in values causes stability problems in calculating Bernoulli numbers using certain algorithms. Starting with the wealth of equations for the Bernoulli numbers in Wikipedia, I began trying various equations presented there (and I recommend you look at Wikipedia's web pages). After implementing various algorithms, observing the stability of their results, I found the following: 1. The recursive method were dead on in giving accurate results. 2. The series approximation method and the two matrixbased equations gave very good accuracynot perfect but very good for the computation effort and code involved. 3. The rest of methods, like double summations, the Akiyama–Tanigawa method, and a variant of the Akiyama–Tanigawa method that I stumbled upon on the Internet, worked well for up to Bernoulli(10) and then gave bad results for higher numbers!!! I implemented one of the matrix methods that give results that are close to the exact values. The code enjoys simplicity compared to implementing other algorithms (which I did in Excel VBA):
EXPORT BernMat(n) Calculating the coefficients of the Bernoulli polynomial is very simple once you have the Bernoulli numbers!! Enjoy!! Namir
Edited: 21 Nov 2013, 3:23 p.m. after one or more responses were posted
Re: A fast Bernoulli Number method for the HP Prime  Alberto Candel  11212013 Hi, Re: A fast Bernoulli Number method for the HP Prime  parisse  11212013 It computes the Bernoulli number, the command bernoulli is available in Xcas but for some obscure reason it is not on Prime. The code uses the recurrence relation, it stores intermediate result in a list to avoid recursive calls:
gen bernoulli(const gen & x){ Re: A fast Bernoulli Number method for the HP Prime  Han  11212013 Quote:
Added formatting to help make it more legible.
Re: A fast Bernoulli Number method for the HP Prime  Ángel Martin  11212013 That's also how I did it on the SandMath  see below. n * Zeta (1n) =  Bn
01 LBL "BN2" Edited: 21 Nov 2013, 11:48 a.m.
Re: A fast Bernoulli Number method for the HP Prime  Alberto Candel  11212013 If I understand correctly, Zeta(2n) is computed via Euler's identity Zeta(2n)= (+)B(2n)*(2pi)^2n /2(2n!). How are other values of Zeta computed? Like Apery's Zeta(3). Thanks
Re: A fast Bernoulli Number method for the HP Prime  Namir  11212013 Alberto, Check Wikipedia's Bernoulli Number web page and look for the expressions for Bn that use matrices.
Namir
Re: A fast Bernoulli Number method for the HP Prime  Paul Dale  11212013 The 34S uses its implementation of the Zeta function to calculate the Bernoulli numbers.
Re: A fast Bernoulli Number method for the HP Prime  Namir  11212013 Here is the HP Prime program that calculates Bernoulli number using series approximation:
EXPORT BernSer(n,toler) NOTE: I updated the listing tweak it.
Edited: 23 Nov 2013, 10:41 a.m. after one or more responses were posted
Re: A fast Bernoulli Number method for the HP Prime  Joe Horn  11222013 Quote: Cool. So, on Prime, define this in CAS: Bernoulli(x) := –x*Zeta(1–x) Cranks out Bernoulli(200), all 224 digits over 7 digits, in about 3.7 seconds on the hardware. Edit: The above gets the wrong answer for Bernoulli(0). Gotta add that as an exception. It should return 1. Edit 2: Bernoulli(x) := IFTE(x=0,1,–x*Zeta(1–x))
Edited: 22 Nov 2013, 7:55 a.m.
Re: A fast Bernoulli Number method for the HP Prime  Gerson W. Barbosa  11222013 Great! This works also on my HP 50g with this Riemann's Zeta function implementation:
<< 199 Zeta 200 * NEG >> TEVAL > 3.64707726448E215 ( 0.47 s)The HP28S version returns 3.64707724956E215 in a couple of seconds.
Re: A fast Bernoulli Number method for the HP Prime  Namir  11222013 Joe, The availability of the Zeta function makes evaluating Bernoulli numbers and polynomials very trivial. The code I presented on this site calculates the Bernoulli numbers using simpler functions (well .. if you can call the matrix determinant simple ... :)). My second listing uses more basic operations to calculate the Bernoulli numbers. I should be able to post a listing for the HP41C soon (I am sure Jm Balliard has done that already)!! Even an HP67 version is doable.
It seems that the HP Prime and the wp34s calculators share this advantage!! Re: A fast Bernoulli Number method for the HP Prime  Alberto Candel  11222013 Hi all, a
[added] my bad. I just saw some of the code for zeta in the post by Gerson above. Thanks
Edited: 22 Nov 2013, 11:20 a.m.
Re: A fast Bernoulli Number method for the HP Prime  Gerson W. Barbosa  11222013 Hi Alberto, If you've followed the links you may have found another method, Borwein's algorithm as implemented by Thomas Ritschel in message #5 here: http://www.hpmuseum.org/cgisys/cgiwrap/hpmuseum/archv021.cgi?read=235199 That one is better, but I chose the older method because it was easier to follow in the reference I had (message #20 in this thread). I've used fix parameters that grant accuracy in the real domain only. Arguments with absolute imaginary parts greater than 50 or so would require more terms. That was enough for my needs, but it's not a definitive zeta implementation for complex arguments yet. Gerson.
Edited: 22 Nov 2013, 1:41 p.m.
Re: A fast Bernoulli Number method for the HP Prime  Namir  11222013 And here is the HP41C implementation for Bernoulli numbers using series approximation. It requires no modules or synthetic commands:
LBL "BERSER" Enjoy!
Edited: 23 Nov 2013, 10:52 a.m.
Re: A fast Bernoulli Number method for the HP Prime  Alberto Candel  11222013 Hi Namir, Thanks, nice, just tried it (I don't have a 41 thou). Is there a way to modify the code so as to output the actual number as a fraction?
a
Re: HP Prime numerical restrictions?  Alasdair McAndrew  11252013 There are no known closed form expressions for odd values of the zeta function. Apery was able to prove that zeta(3) was irrational, and that's pretty much as far as people know about zeta(2n+1). According to the wikipedia page, infinite values of zeta(2n+1) are irrational, and at least one of zeta(5), zeta(7), zeta(9), zeta(11) is irrational. But no closed forms!
