Created
October 9, 2009 00:23
-
-
Save ChrisMissal/205559 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
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