Finding a number based on how many loops of the Collatz Conjecture it takes to reach 1

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

Answers (1)

M. Nasir Javaid
M. Nasir Javaid

Reputation: 5990

Collatz Conjecture's example is:

Consider, we have a number 7 and we need to reach 1 using the Collatz Conjecture

  1. 7 is odd so we use the algorithm 3(7) + 1 = 22
  2. 22 is even so we use 22/2 = 11
  3. 11 is odd so we use the algorithm 3(11) + 1 = 34
  4. 34 is even so we use the algorithm 34/2 = 17
  5. 17 is odd so we use the algorithm 3(17) + 1 = 52
  6. 52 is even so we use the algorithm 52/2 = 26
  7. 26 is even so we use the algorithm 26/2 = 13
  8. 13 is odd so we use the algorithm 13(3) + 1 = 40
  9. 40 is even so we use the algorithm 40/2 = 20
  10. 20 is even so we use the algorithm 20/2 = 10
  11. 10 is even so we use the algorithm 10/2 = 5
  12. 5 is odd so we use the algorithm 5(3) + 1 = 16
  13. 16 is even so we use the algorithm 16/2 = 8
  14. 8 is even so we use the algorithm 8/2 = 4
  15. 4 is even so we use the algorithm 4/2 = 2
  16. 2 is even so we use the algorithm 2/2 = 1

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

Related Questions