Reputation: 546
Basically, I want to write an algorithm to find out which number takes 500 iterations to reach 1. I tried some variations but couldn't get it right.
Here is my code so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sequence4
{
class Program
{
static void Main(string[] args)
{
long startingNumber = 1;
long count = 0;
while (count != 500)
{
startingNumber = startingNumber * 2;
count++;
startingNumber = startingNumber / 3 - 1;
count++;
}
Console.WriteLine(count);
Console.WriteLine(startingNumber);
}
}
}
EDIT: Updated version of the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sequence4
{
class Program
{
static void Main(string[] args)
{
int number = 2;
int count = 0;
while (count != 500)
{
if (number % 2 == 0)
{
number = 2 * number;
count++;
}
if (number % 2 != 0)
{
number = (number / 3) - 1;
count++;
}
}
Console.WriteLine(number);
Console.WriteLine(count);
}
}
}
Upvotes: 1
Views: 781
Reputation: 5990
Collatz Conjecture's example is:
Consider, we have a number 7 and we need to reach 1 using the Collatz Conjecture
For Odd Number: x = 3n + 1
For Even Number: x = n / 2
We have applied the algorithm 16 times for the number 7 and we got to 1. So, 16 is the cycle length.
Now, if we take above example, we need to move reverse from bottom line to 500 times upward. For reverse iterations, we use:
For Odd Number: x = (n - 1) / 3
For Even Number: x = n * 2
Now, programmatically, implement as:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double output = 1;
const int iterations = 500;
for (var i = 1; i <= iterations; i++)
{
output = GetOutput(output);
Console.WriteLine("Number after {0} iterations is: {1}", i, output);
}
Console.WriteLine("Required Number is: {0}", output);
VerifyResult(output, iterations);
Console.ReadKey();
}
private static double GetOutput(double input)
{
if (input == 1)
{
return 2;
}
var output = (input - 1) / 3;
return output % 1 == 0 && output % 2 != 0 && output > 3 ? output : input * 2;
}
//To verify the above results we need this method
private static void VerifyResult(double output, int iterations)
{
//-------------------------VERIFICATION-----------------------
Console.WriteLine("Press any key to check iterations in reverse");
Console.ReadKey();
Console.WriteLine("Running validation process ...");
var n = output;
var max = n;
var count = 0;
Console.WriteLine("{0} (starting number in Collatz Sequence)", n);
while (n > 1)
{
n = n % 2 == 0 ? n / 2 : 3 * n + 1;
count++;
if (n > max) max = n;
Console.WriteLine(n);
}
if (count == iterations) //match here iterations and outputs
{
Console.WriteLine("\n\nCONGRATULATION! Verification results matched. :-)\n\n");
Console.WriteLine("There are {0} cycle length in the sequence", count);
Console.WriteLine("The largest number in the sequence is {0}", output);
Console.WriteLine("\n\n-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-");
Console.WriteLine("\n\nREQUIRED NUMBER: {0}\n\n", output);
Console.WriteLine("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n");
Console.WriteLine("\nPress any key to exit");
}
else
{
Console.WriteLine("Oops... Verification results are not matching. :-(");
}
}
}
}
Example's Source: Algorithm guidance with 3n+1 Conjecture
Upvotes: 2