From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Paul Rubin Newsgroups: comp.lang.ada Subject: Re: Bernoulli numbers at RosettaCode Date: Fri, 10 Feb 2017 16:07:43 -0800 Organization: A noiseless patient Spider Message-ID: <87fujlvbcg.fsf@nightsong.com> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="2da6cbab8334358691de616e9680597a"; logging-data="29401"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX187eJELO6NWqa5/j9CWjtyH" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:pPzwNuDMr0bHU88/G/8TRD+j0QE= sha1:bQ/ZkqhoQlguehVcaJErMrpLRzs= Xref: news.eternal-september.org comp.lang.ada:33319 Date: 2017-02-10T16:07:43-08:00 List-Id: "Alejandro R. Mosteo" 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 #include 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 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); }