Created
March 15, 2014 19:46
-
-
Save kashwaa/9572836 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Diagnostics; | |
namespace pandigital | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
UInt64 startnum = 1; | |
List<UInt64> results = new List<UInt64>(); | |
do | |
{ | |
for (int i =2; i < 10; i++) | |
{ | |
UInt64 x=concat(startnum,generateSec(i)); | |
if (x > 999999999) break; | |
if (is9PanDigital(x)) | |
{ | |
results.Add(x); | |
} | |
} | |
startnum++; | |
} while (getNumCount(startnum)<=5); | |
sw.Stop(); | |
Console.WriteLine("Result is:{0}",results.Max()); | |
Console.WriteLine("Runtime:{0} Millicesonds",sw.ElapsedMilliseconds); | |
} | |
public static UInt64 concat(UInt64 i, List<int> l) | |
{ | |
UInt64 res = 0; | |
UInt64 p = 0; | |
for (int n =l.Count-1; n >=0 ; n--) | |
{ | |
p = i * (UInt64)l[n] * (p == 0 ? 1 : (UInt64)Math.Pow(10, getNumCount(p))); | |
res += p; | |
} | |
return res; | |
} | |
public static UInt64 getNumCount(UInt64 i) | |
{ | |
UInt64 n = 0; | |
while (i > 0) | |
{ | |
n++; | |
i /= 10; | |
} | |
return n; | |
} | |
public static List<int> generateSec(int i) | |
{ | |
List<int> res = new List<int>(); | |
for (int x = 0; x < i; x++) | |
{ | |
res.Add(x + 1); | |
} | |
return res; | |
} | |
public static bool is9PanDigital(UInt64 i) | |
{ | |
UInt64[] temp = new UInt64[9]; | |
for (UInt64 x = 0; x < 9; x++) | |
{ | |
UInt64 n = i % 10; | |
if (n == 0) return false; | |
if (temp.Contains(n)) return false; | |
temp[x] = n; | |
i /= 10; | |
} | |
if (i != 0) return false; | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment