public
anonymous / gist:4976509
Created

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
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);
}
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.