#include <map>
using namespace std;

int threeN(int n)
{
    if (n % 2 == 0)
        return n / 2;
    else
        return n * 3 + 1;
}
int getLength(int n)
{
    static map<int, int> history;
    int key = n;
    if (n == 1)
        return 1;
    if (history.find(key) != history.end())
        return history[key];
    int length = getLength(threeN(n)) + 1;
    history[key] = length;
    return length;
}

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *