Skip to content

Instantly share code, notes, and snippets.

Created February 18, 2013 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4976509 to your computer and use it in GitHub Desktop.
Save anonymous/4976509 to your computer and use it in GitHub Desktop.
namespace NineDigits
{
class Program
{
static void Main(string[] args)
{
var p = new Program();
while (true)
{
p.RunTest();
var text = Console.ReadLine();
if (text == "exit")
break;
}
}
public void RunTest()
{
Numbers = new List<long>();
Stopwatch sw = new Stopwatch();
sw.Start();
Calculate(0, 0);
sw.Stop();
decimal time = sw.ElapsedTicks / (decimal)Stopwatch.Frequency * 1000;
Console.WriteLine("Time: " + time);
}
public List<long> Numbers;
public void Calculate(long x, int len)
{
if (len > 9)
return;
int start = (x == 0) ? 1 : 0;
for(int i = start; i <= 9; i++)
{
var temp = x * 10 + i;
if (temp > 0 && temp % (len + 1) == 0)
{
if (len + 1 == 9)
Numbers.Add(temp);
else
Calculate(temp, len + 1);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment