25. July 2011 18:00
Another interview question which can be suitable for a test to perform some simple calculations to verify an answer. The question is to find the smallest positive number that is evenly divisible by all the numbers from 1 to 20.
You answer should look something like this.
static void Main(string[] args)
{
DateTime Start = DateTime.Now;
int Answer = 0;
do
{
Answer++;
} while (CanDiv(Answer, 20) == false);
Console.WriteLine("Answer: {0}", Answer);
Console.WriteLine("Time Taken: {0}", DateTime.Now - Start);
Console.ReadLine();
}
static bool CanDiv(int value, int max)
{
for (int i = max - 1; i > 0; i--)
if (value % i != 0)
return false;
return true;
}
The CanDiv function could also do the same calculation with a for loop counting up. However it will result in taking longer to run.
static bool OldCanDiv(int value, int max)
{
for (int i = 1; i < max; i++)
if (value % i != 0)
return false;
return true;
}
60acf657-2d99-459f-8a66-902fb49443aa|0|.0