Skip to content

Instantly share code, notes, and snippets.

@pb-uk
Created August 13, 2020 15:14
Show Gist options
  • Save pb-uk/8be6c386e2da4b654cd03a41731415a7 to your computer and use it in GitHub Desktop.
Save pb-uk/8be6c386e2da4b654cd03a41731415a7 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
#define M 3;
int main() {
int step = 0;
uint64_t n = 2;
uint64_t term = 2;
uint64_t stepsRecord = 0;
uint64_t termRecord = 0;
uint64_t maxSafeTerm = 0xffffffffffffffff / 3;
bool isTermRecord = false;
while (true) {
step++;
if (term % 2 == 0) {
// The term is even so divide it by 2.
term /= 2;
if (term == 1) {
// The sequence has collapsed to 1.
if (step > stepsRecord) {
// New record!
stepsRecord = step;
cout << "Step record " << n << ',' << step << endl;
}
if (isTermRecord) {
// New record!
cout << "Term record " << n << ',' << termRecord << endl;
}
// Move on to the next N.
n++;
term = n;
step = 0;
isTermRecord = false;
}
} else {
if (term > maxSafeTerm) break;
// The term is odd so do the m.n + 1 calculation.
term = term * 3 + 1;
if (term > termRecord) {
// New record - but check for overflow first.
// if (term > Number.MAX_SAFE_INTEGER) {
// throw new Error('Overflow');
// }
termRecord = term;
isTermRecord = true;
}
}
}
cout << "Stopped at " << n;
cout << " with term " << term;
cout << " on step " << step << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment