Created
February 27, 2019 08:50
-
-
Save hempnall/4f18db1e0f18ee7317a6f0ead414af26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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