[Edu-sig] Bernoulli Numbers (a generator)
kirby.urner at gmail.com
Thu Jul 16 00:06:58 CEST 2009
Yes, much cooler, thanks!
On Wed, Jul 15, 2009 at 1:16 PM, Gregor Lingl<gregor.lingl at aon.at> wrote:
> Very interesting issue, superbly documented by Kirby, thanks!
> May I contribute a slightly streamlined version of the seidel-generator:
> see attachment. (Please check if everything is correct.)
> kirby urner schrieb:
>> Just adding to what's on file, not sure this is a "must have" but it's
>> fun to do, especially given the association of Bernoulli Numbers with
>> "the first computer program" as scoped out in Ada's notes:
>> http://www.flickr.com/photos/17157315@N00/3718903352/sizes/o/ (shell
>> view, Akbar font)
>> http://www.4dsolutions.net/ocn/python/ada.py (source code)
>> According to Wikipedia, this is similar to what Knuth recommends i.e.
>> we're staying with integer operations, although I'm using the Fraction
>> type to output results (Bernoulli numbers are fractions with lots of
>> digits once you get into 'em, ideal grist for the mill when learning
>> about Python generators).
> Python version: 3.1
> author: Kirby Urner, 4D Solutions
> release: 1.01, July 15, 2009
> quick and dirty implementation of Bernoulli numbers
> as a generator, getting clues from Wikipedia article
> code of seidel generator slightly streamlined by Gregor Lingl
> "to Ada Byron, with respect and admiration"
> (hence name ada.py)
> "In note G of Ada Lovelace's notes on the
> Analytical engine from 1842, Lovelace
> describes an algorithm for generating
> Bernoulli numbers with Babbage's machine [~ 1].
> As a result, the Bernoulli numbers have
> the distinction of being the subject of
> the first computer program."
> Wikipedia: http://en.wikipedia.org/wiki/Bernoulli_number
> Source code (current version):
> from fractions import Fraction
> def seidel():
> "...in 1877 Philipp Ludwig von Seidel published an ingenious
> algorithm which makes it extremely simple to calculate Tn."
> (Ibid, Wikipedia)
> See OEIS:
> yield 1
> yield 1
> row = 
> while True:
> t = 0
> newrow = 
> for i in row:
> t += i
> yield t
> row = reversed(newrow)
> def bernoulli():
> yield Fraction(1,1)
> yield Fraction(-1,2)
> seidelgen = seidel()
> sign = 1
> n = 2
> while True:
> sign *= -1
> denom = 2**n -4**n
> numer = sign * n * next(seidelgen)
> n += 2
> yield Fraction(numer, denom)
More information about the Edu-sig