Sylvester's sequence: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: changed a comment. |
Added C++ solution |
||
Line 66:
165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of reciprocols: +9.99999999999999999999999999999999999999999...999999999999999999999999999964e -1
</pre>
=={{header|C++}}==
{{libheader|Boost}}
<lang cpp>#include <iomanip>
#include <iostream>
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using integer = boost::multiprecision::cpp_int;
using rational = boost::rational<integer>;
integer sylvester_next(const integer& n) {
return n * n - n + 1;
}
int main() {
std::cout << "First 10 elements in Sylvester's sequence:\n";
integer term = 2;
rational sum = 0;
for (int i = 1; i <= 10; ++i) {
std::cout << std::setw(2) << i << ": " << term << '\n';
sum += rational(1, term);
term = sylvester_next(term);
}
std::cout << "Sum of reciprocals: " << sum << '\n';
}</lang>
{{out}}
<pre>
First 10 elements in Sylvester's sequence:
1: 2
2: 3
3: 7
4: 43
5: 1807
6: 3263443
7: 10650056950807
8: 113423713055421844361000443
9: 12864938683278671740537145998360961546653259485195807
10: 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
Sum of reciprocals: 27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920805/27392450308603031423410234291674686281194364367580914627947367941608692026226993634332118404582438634929548737283992369758487974306317730580753883429460344956410077034761330476016739454649828385541500213920806
</pre>
|