Skip to content

Instantly share code, notes, and snippets.

@david-batranu
Created October 23, 2013 18:33
Show Gist options
  • Save david-batranu/7124059 to your computer and use it in GitHub Desktop.
Save david-batranu/7124059 to your computer and use it in GitHub Desktop.
/*
* main.cpp
*
* Created on: Oct 23, 2013
* Author: david
*/
#include <iostream>
#include <map>
#include <stdio.h>
std::map<unsigned long, int> collatz;
int Collatz(unsigned long n){
if(!collatz[n]){
if(n % 2 == 0){
collatz[n] = Collatz(n/2);
} else {
collatz[n] = Collatz(3*n + 1);
}
}
return collatz[n] + 1;
}
int biggest = 0;
int chain = 0;
void find_biggest(){
for(unsigned long i = 0; i < collatz.size(); i++){
int value = collatz[i];
if(value > chain){
biggest = i;
chain = value;
}
}
}
int main(){
collatz[1] = 1;
for(int j = 10000; j > 0; j--){
Collatz(j);
}
find_biggest();
fprintf(stdout, "Biggest:%i Chain:%i\n", biggest, chain);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment