comp.lang.ada
 help / color / mirror / Atom feed
From: Paul Rubin <no.email@nospam.invalid>
Subject: Re: Bernoulli numbers at RosettaCode
Date: Fri, 10 Feb 2017 16:07:43 -0800
Date: 2017-02-10T16:07:43-08:00	[thread overview]
Message-ID: <87fujlvbcg.fsf@nightsong.com> (raw)
In-Reply-To: o7kr00$qcn$1@dont-email.me

"Alejandro R. Mosteo" <alejandro@mosteo.com> writes:
> Funnily enough, the Pascal impl is taken from an Ada one:
> https://marquisdegeek.com/code_ada99

The point of the exercise seems to be to do it all with multi-precision
arithmetic, and it doesn't look to me like any of the Pascal solutions
attempted that.  Here's my C++11 solution which simply follows the
algorithm on the Rosetta site:

    #include <boost/multiprecision/gmp.hpp>
    #include <iostream>
    using rational = boost::multiprecision::mpq_rational;

    rational bernoulli(int);

    int main() {
      for (int i = 1; i <= 60; i++) {
        rational b = bernoulli(i);
        if (b != 0)
          std::cout << i << "\t" << b << "\n";
      }
    }

    rational bernoulli(int n) {
      std::vector<rational> a;
      for (int m = 0; m <= n; m++) {
        a.push_back(rational(1,m+1));
        for (int j = m; j >= 1; j--)
          a.at(j-1) = j * (a.at(j-1) - a.at(j));
      }
      return a.at(0);
    }

      reply	other threads:[~2017-02-11  0:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-10 16:51 Bernoulli numbers at RosettaCode Alejandro R. Mosteo
2017-02-11  0:07 ` Paul Rubin [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox