Created
March 28, 2016 15:26
-
-
Save compustar/697fae178cb5bdd6d9d1 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.Linq; | |
| using System.Text; | |
| public class Program { | |
| public static void Main(string[] args) { | |
| var @base = 16; | |
| var pLength = 4; | |
| var n = (pLength - 1) * 4; | |
| var max = Max(pLength - 1, @base); | |
| var min = Min(pLength - 1, @base); | |
| var count = 0; | |
| System.Diagnostics.Stopwatch timer = System.Diagnostics.Stopwatch.StartNew(); | |
| foreach (var ppp in Enumerable.Range(Min(pLength, @base), Max(pLength - 1, @base) * 2).Select(o => IntToString(o, @base)).Where(o => o.All(p => o.First() == p)).Select(o => StringToInt(o, @base))) | |
| for (int ef = min; ef <= max; ef++) { | |
| var p = IntToString(ppp, @base).First(); | |
| int gh = ppp - ef; | |
| for (int cd = min; cd <= max; cd++) { | |
| int ab = ef + cd; | |
| if (new int[] { ef, gh, ab, cd }.SelectMany(o => IntToString(o, @base)).Distinct().Where(o => o != p).Count() == n && gh >=min && gh <= max && ab >= min && ab <= max) { | |
| count++; | |
| } | |
| } | |
| } | |
| timer.Stop(); | |
| Console.WriteLine(timer.Elapsed + " - " + count); | |
| Console.ReadLine(); | |
| } | |
| static string digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"; | |
| public static int Min(int length, int @base) { | |
| return StringToInt("1".PadRight(length, '0'), @base); | |
| } | |
| public static int Max(int length, int @base) { | |
| return StringToInt(digits[@base - 1].ToString().PadRight(length, digits[@base - 1]), @base); | |
| } | |
| public static int StringToInt(string value, int @base) { | |
| var result = 0; | |
| for (int i = 0, n = value.Length; i < n; i++) { | |
| result += digits.IndexOf(value[i]) * (int)Math.Pow(@base, n - 1 - i); | |
| } | |
| return result; | |
| } | |
| public static string IntToString(int value, int @base) { | |
| StringBuilder buffer = new StringBuilder(); | |
| for (; value > 0; value = value / @base) buffer.Append(digits[value % @base]); | |
| return new String(buffer.ToString().Reverse().ToArray()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment