Skip to content

Instantly share code, notes, and snippets.

@ChrisMissal
Created October 9, 2009 00:23
Show Gist options
  • Save ChrisMissal/205559 to your computer and use it in GitHub Desktop.
Save ChrisMissal/205559 to your computer and use it in GitHub Desktop.
public static Int32 GetChainLength(Int32 value)
{
Func<Int32, Int32> a = n => n / 2;
Func<Int32, Int32> b = n => 3 * n + 1;
Int32 chainLength = 1;
Int32 x = value;
while(x > 1)
{
x = (x > 1 && x % 2 == 0) ? a(x) : b(x);
chainLength++;
if (x < 0)
return 0;
if (x == 1)
return chainLength;
}
return chainLength;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment