Created
November 5, 2022 23:04
-
-
Save fabiomaulo/ea2577261f8d4c29b6de2ae2fd0ab060 to your computer and use it in GitHub Desktop.
The famous 3X + 1
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
internal class Program | |
{ | |
private const ulong limit = 1; | |
private static void Main(string[] args) | |
{ | |
if (args.Length < 1) | |
{ | |
Console.WriteLine("No start number specified"); | |
return; | |
} | |
if (!ulong.TryParse(args[0], out ulong inputNumber)) | |
{ | |
Console.WriteLine("No valid start number: {0}", args[0]); | |
return; | |
} | |
ulong iterations = 0; | |
ulong maxvalue = inputNumber; | |
ulong actual = inputNumber; | |
while (actual > limit) | |
{ | |
iterations++; | |
if ((actual & limit) == limit) | |
{ | |
actual *= 3; | |
actual++; | |
} | |
else | |
{ | |
actual >>= 1; | |
} | |
maxvalue = Math.Max(actual, maxvalue); | |
} | |
Console.WriteLine("{0} solved in {1} iterations", inputNumber, iterations); | |
Console.WriteLine("Max value riched was {0}.", maxvalue); | |
} | |
} |
Author
fabiomaulo
commented
Nov 5, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment