Question #38908

Find all the combinations between 1 and 1000 in which


1. doubling the first number and substracting from the second number will give a result of zero.


2. if the result is not 0, double the result and use it as the first number and repeat step 1.

Expert's answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Q38908
{
    class Program
    {
        static int result;
        static bool flag = true;
        static List<int> list = new List<int>();
        static void Main(string[] args)
        {
            result = 0;
            for (int i = 1; i <= 1000; i++)
                for (int j = i + 1; j <= 1000; j++)
                {
                    Func(i, j);
                }
            Console.ReadKey();
        }
        static void Func(int i, int j)
        {
            result = j - 2 * i;
            if (result == 0)
            {
                if (list.Contains(i)) return;
                list.Add(i);
                Console.Write("|{0,9}{1,9}|", i, j);
                return;
            }
            if (result > 0 && flag)
            {
                flag = false;
                Func(result, j);
            }
            flag = true;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS