Find the smallest divisible number for the given input 'n' such that number is evenly divisible by 1 to n Given a number n, the task is to complete the function which returns an integer denoting the smallest number evenly divisible by each number from 1 to n.

I have solved it using the following function:

long long getSmallestDivNum(long long n)
{
   //number should be multiplier of n
    bool found = true;
    int i = 2;
    while(1)
    {
        long long r = n*i;
        found = true;
        for(int j=2;j&lt;=n;j++)
        {
            if( r % j != 0)
            {
                found  = false;
            }
        }
        if(found != true)
        {
            i++;
        }
        else
        {
            return r;
        }
    }
}


But I am getting a time-limit-exceeded-error like this:


  Run Time Error Time Limit of 5 seconds exceeded.


When I execute the code in my local NetBeans, it works fine with a runtime of 62ms.
Your solution to the problem is "check every number to see if it has that property", but that's a lot of numbers. You can do a lot better.

Let me give you a big hint.

Suppose you want find the solution for n == 9. Suppose you had the solution for n == 8 is 840.  Don't worry about how you know that. 

Given the fact that 840 is the smallest number that is divisible by 1, 2, 3, 4, 5, 6, 7 and 8, and that 9 is 3 times 3, how do you find a number that is divisible by 1, 2, 3, 4, 5, 6, 7, 8 and 9?  Plainly multiplying 840 by 9 gives a number that is too large, because 9 is already 3 x 3.  We only need to multiply by 3. So the solution for 9 is 2520.

Now suppose we want to find the solution for 10. We already know the solution for 9, and hey, it is already divisible by 10. So we're done.

Now suppose we want to find the solution for 11.  2520 is not divisible by 11, and 11 is prime.  So we have to multiply by 11.

What about 12?  12 is 3 x 4, and we already have a number that is divisible by both 3 and 4, so the solution for 11 is the same as the solution for 12.  

And so on.

So is it now clear how to solve this problem efficiently?

UPDATE

Apparently it was not clear.

Think about it this way.  


The solution for 2 is 2.
The solution for 3 needs to have a factor of 3, but the solution for 2 has no factor of 3. So the solution for 3 is 2 x 3.
The solution for 4 needs two factors of 2. The solution for 3 has only one factor of 2, so we add an additional factor of two: 2 x 3 x 2.
The solution for 5 needs a factor of 5, but we don't have one in the solution for 4. So 2 x 3 x 2 x 5.
The solution for 6 needs a factor of 2 and a factor of 3. The solution for 5 has a 2 and a 3 already, so 2 x 3 x 2 x 5 works for 6.
The solution for 7 needs a factor of 7, but the solution for 6 doesn't have one.  So 2 x 3 x 2 x 5 x 7.
The solution for 8 needs three factors of 2, but the solution for 7 only has two.  So 2 x 3 x 2 x 5 x 7 x 2.
The solution for 9 needs two factors of 3, but the solution for 8 only has one.  So 2 x 3 x 2 x 5 x 7 x 2 x 3.
The solution for 10 needs a factor of 2 and a factor of 5. The solution for 9 already has those. So 2 x 3 x 2 x 5 x 7 x 2 x 3.
Now do you see the pattern?