[Edu-sig] Bernoulli Numbers (a generator)
kirby urner
kirby.urner at gmail.com
Thu Jul 16 00:06:58 CEST 2009
Yes, much cooler, thanks!
Kirby
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.)
>
> Regards,
> Gregor
>
>
> 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).
>>
>> Kirby
>> 4D
>>
>>
>
> """
> 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
>
>
> Dedication:
> "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):
> http://www.4dsolutions.net/ocn/python/ada.py
>
> """
>
> 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:
> http://www.research.att.com/~njas/sequences/A000111
> """
> yield 1
> yield 1
> row = [1]
> while True:
> t = 0
> newrow = []
> for i in row:
> t += i
> newrow.append(t)
> newrow.append(t)
> yield t
> row = reversed(newrow)
>
> def bernoulli():
> yield Fraction(1,1)
> yield Fraction(-1,2)
> seidelgen = seidel()
> next(seidelgen)
> sign = 1
> n = 2
> while True:
> sign *= -1
> denom = 2**n -4**n
> numer = sign * n * next(seidelgen)
> next(seidelgen)
> n += 2
> yield Fraction(numer, denom)
>
>
More information about the Edu-sig
mailing list