Skip to content

Instantly share code, notes, and snippets.

@hempnall
Created February 27, 2019 08:50
Show Gist options
  • Save hempnall/4f18db1e0f18ee7317a6f0ead414af26 to your computer and use it in GitHub Desktop.
Save hempnall/4f18db1e0f18ee7317a6f0ead414af26 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <map>
using namespace std;
#define MILLION 1000000
uint64_t collatz_func( uint64_t num )
{
if (num % 2 == 0) {
return num / 2 ;
} else {
return ( num * 3 ) + 1;
}
}
uint64_t collatz_seq( uint64_t n ) {
if ( n == 1) {
return 0;
} else {
return 1 + collatz_seq( collatz_func( n ) ) ;
}
}
int main()
{
uint64_t maxlen = 0;
uint64_t maxidx =0;
for (int i = 1 ; i < MILLION ; ++i ) {
uint64_t len = collatz_seq( i ) ;
if (len > maxlen) { maxlen = len; maxidx = i; }
}
std::cout << "i=" << maxidx << " len=" << maxlen << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment