Bernoulli numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments and whitespace, simplified some statements, optimized some functions.)
m (→‎{{header|C++}}: works with)
Line 769: Line 769:


=={{header|C++}}==
=={{header|C++}}==
{{Works with|C++11}}

=== Using Boost | C++11 ===
{{libheader|boost}}
{{libheader|boost}}
<lang cpp>
<lang cpp>/**

/**
* Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
* Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
* Apple LLVM version 9.1.0 (clang-902.0.39.1)
* Apple LLVM version 9.1.0 (clang-902.0.39.1)
Line 781: Line 778:
*/
*/


#include <iostream> //std::cout
#include <boost/multiprecision/cpp_int.hpp> // 1024bit precision
#include <iostream> //formatting
#include <boost/rational.hpp> // Rationals
#include <vector> //Container
#include <iostream> // std::cout
#include <boost/rational.hpp> // Rationals
#include <iostream> // formatting
#include <vector> // Container
#include <boost/multiprecision/cpp_int.hpp> //1024bit precision


typedef boost::rational<boost::multiprecision::int1024_t> rational; // reduce boilerplate


rational bernoulli(size_t n) {
typedef boost::rational<boost::multiprecision::int1024_t> rational; // reduce boilerplate
auto out = std::vector<rational>();


rational bernulli(size_t n){
for (size_t m = 0; m <= n; m++) {
out.emplace_back(1, (m + 1)); // automatically constructs object

for (size_t j = m; j >= 1; j--) {
auto out = std::vector<rational>();
out[j - 1] = rational(j) * (out[j - 1] - out[j]);

}
for(size_t m=0;m<=n;m++){
}
out.emplace_back(1,(m+1)); // automatically constructs object
return out[0];
for (size_t j = m;j>=1;j--){
}
out[j-1] = rational(j) * (out[j-1]-out[j]);
}
}
return out[0];
}


int main() {
int main() {
for(size_t n = 0; n <= 60;n+=n>=2?2:1){
for (size_t n = 0; n <= 60; n += n >= 2 ? 2 : 1) {
auto b = bernulli(n);
auto b = bernoulli(n);
std::cout << "B("<<std::right<<std::setw(2)<<n<<") = ";
std::cout << "B(" << std::right << std::setw(2) << n << ") = ";
std::cout << std::right<<std::setw(44)<<b.numerator();
std::cout << std::right << std::setw(44) << b.numerator();
std::cout << " / " << b.denominator() <<std::endl;
std::cout << " / " << b.denominator() << std::endl;
}
}


return 0;
return 0;
}</lang>
}
</lang>
{{out}}
{{out}}
<pre>
<pre>