Search code examples
c#algorithmdivision

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20


My problem is the answer of this code. I know it's working but the answer is taking so long and the numbers still going.

class Program
{
    static void Main(string[] args)
    {
        long Number = 1;
        for (int i = 1; i <= 20; i++)
        {
            if (Number % i != 0)
            {
                i = 1;
                Number++;
            }
            Console.WriteLine(Number);
        }
        Console.Read();
    }
}

I want the console write the last number but i dont know what to do.


Solution

  •  static void Main(string[] args)
            {
                Console.WriteLine(problem().ToString());
    
    }
    
        public static int problem()
                {
                    int i =1;
                    bool found = false;
                    while (!found)
                    {
                        i += 1;
                        bool isDividable = true;
                        for (int j = 1; j <= 20; j++)
                        {
                            if (i % j != 0)
                            {
                                isDividable = false;
                                break;
                            }
                        }
                        if (isDividable)
                        {
                            found = true;
                        }
                    }
                    return i;
                }