Skip to content

Instantly share code, notes, and snippets.

@nathanPro
Last active August 29, 2015 14:01
Show Gist options
  • Save nathanPro/ff7af69adfe0437b3f60 to your computer and use it in GitHub Desktop.
Save nathanPro/ff7af69adfe0437b3f60 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <map>
using namespace std;
int getCollatz(long a, map<long,int> &source)
{
if( source.find(a) == source.end() )
{
if(a%2 == 0)
source[a] = 1 + getCollatz(a/2, source);
else
source[a] = 1 + getCollatz(3*a+1, source);
}
return source[a];
}
int main()
{
map<long, int> collatz;
int steps;
long input;
collatz[1] = 1;
while ( scanf("%li", &input) > 0 ){
steps = getCollatz( input, collatz);
printf("%li in %d steps.\n", input, steps);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment