Hofstadter Figure-Figure sequences: Difference between revisions

Content added Content deleted
m (→‎REXX Version 2 from PL/I: typo in output)
(Java translated to C++)
Line 303: Line 303:
return 0;
return 0;
}</lang>
}</lang>

=={{header|C++}}==
{{works with|C++|11}}
{{trans|Java}}
<lang cpp>#include <iostream>
#include <set>
#include <vector>

using namespace std;

unsigned hofstadter(unsigned rlistSize, unsigned slistSize)
{
auto rlist = new vector<unsigned> { 1, 3, 7 };
auto slist = new vector<unsigned> { 2, 4, 5, 6 };
auto list = rlistSize > 0 ? rlist : slist;
auto n = rlistSize > 0 ? rlistSize : slistSize;

while (list->size() > n) list->pop_back();

while (list->size() < n)
{
auto lastIndex = rlist->size() - 1;
auto lastr = (*rlist)[lastIndex];
auto r = lastr + (*slist)[lastIndex];
rlist->push_back(r);
for (auto s = lastr + 1; s < r && list->size() < n;)
slist->push_back(s++);
}
return (*list)[n - 1];
}

int main()
{
cout << "R():";
for (auto n = 1; n <= 10; n++) cout << " " << hofstadter(n, 0);
cout << endl;

set<unsigned> r, s;
for (auto n = 1; n <= 960; n++)
{
if (n <= 40)
r.insert(hofstadter(n, 0));
s.insert(hofstadter(0, n));
}

for (auto n = 1; n <= 1000; n++)
if (r.count(n) == s.count(n))
clog << "integer " << n << " either in both or neither set" << endl;
cout << "done" << endl;

return 0;
}</lang>
{{out}}
As Java.


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==