public void GeneratedPermutationsCountTest() { char[] permutable = new char[] { 'A', 'B', 'C', 'D' }; var permutations = Permutations.Generate(permutable); Assert.AreEqual(24, permutations.Count()); }
private void GeneratePermutationsBtn_Click(object sender, EventArgs e) { if (!IsInputValid(InputField.Text)) { MessageBox.Show("Please populate input field by either typing or importing file"); return; } ClearAllFields(); Stopwatch watch = new Stopwatch(); watch.Start(); var permutable = InputField.Text.ToCharArray(); var permutations = Permutations.Generate(permutable); permutations.ToList().ForEach(x => PermutationsField.Text += new string(x) + "\r\n"); PermutationsCountField.Text = permutations.Count().ToString(); Permutations.GetMatches(permutable, permutations).ToList().ForEach(x => MatchingPermutationsField.Text += new string(x) + "\r\n"); watch.Stop(); TimeField.Text = watch.ElapsedMilliseconds.ToString(); }
public override object Run(RunModes runMode, object input, bool Logging) { Primes.InitPrimes(500); List <string> pandigitals = Permutations.Generate("9876543210").ToList(); List <string> pandigitalsIndexProperty = new List <string>(); bool notIndexProperty = false; foreach (var pandigital in pandigitals) { notIndexProperty = false; for (int i = 0; i < 7; i++) { if (Int64.Parse(pandigital.Substring(i + 1, 3)) % Primes.AllPrimes[i] != 0) { notIndexProperty = true; break; } } if (!notIndexProperty) { pandigitalsIndexProperty.Add(pandigital); } } var bigIntList = pandigitalsIndexProperty.Select(item => new BigInt(item)).ToList(); return(new BigInt("").SumBigIntNumbers(bigIntList).Value); }
public override object Run(RunModes runMode, object input, bool Logging) { Primes.InitPrimes((long)Math.Sqrt(987654321)); List <long> permutations = Permutations.Generate(123456789).OrderByDescending(item => item).ToList(); foreach (long permutation in permutations) { if (Primes.AllPrimes.All(item => (permutation != item && permutation % item != 0) || permutation == item)) { return(permutation); } } permutations = Permutations.Generate(12345678).OrderByDescending(item => item).ToList(); foreach (long permutation in permutations) { if (Primes.AllPrimes.All(item => (permutation != item && permutation % item != 0) || permutation == item)) { return(permutation); } } permutations = Permutations.Generate(1234567).OrderByDescending(item => item).ToList(); foreach (long permutation in permutations) { if (Primes.AllPrimes.All(item => (permutation != item && permutation % item != 0) || permutation == item)) { return(permutation); } } return(1); }
public void UniquePermutationsTest() { char[] permutable = new char[] { 'A', 'B', 'C' }; var permutations = Permutations.Generate(permutable); var uniquePermutations = permutations.Distinct(); Assert.AreEqual(permutations.Count(), uniquePermutations.Count()); }
public void MatchingPermutationsCountTest() { char[] permutable = new char[] { 'A', 'B', 'C', 'D' }; var permutations = Permutations.Generate(permutable); var matchingPermutations = Permutations.GetMatches(permutable, permutations); Assert.AreEqual(2, matchingPermutations.Count()); }
public override object Run(RunModes runMode, object input, bool Logging) { Primes.InitPrimes(10000); string givenSolution = input.ToString(); var fourDigitPrimes = Primes.AllPrimes.Where(item => item > 999 && item < 10000).ToList(); foreach (var prime in fourDigitPrimes) { var permutationPrimes = Permutations.Generate(prime).Where(Primes.IsPrime).Where(p => p.ToString().Length == 4).Distinct().OrderBy(p => p).ToList(); int count = permutationPrimes.Count(); if (count < 3) { continue; } string concatPrimes = ""; if (Logging) { for (int i = 0; i < count; i++) { concatPrimes += permutationPrimes[i].ToString(); } Console.WriteLine(concatPrimes); } for (int i = 0; i < count - 2; i++) { for (int j = i + 1; j < count - 1; j++) { var diff = permutationPrimes[j] - permutationPrimes[i]; concatPrimes = permutationPrimes[i].ToString() + permutationPrimes[j].ToString(); for (int k = j + 1; k < count; k++) { if (permutationPrimes[k] - permutationPrimes[j] == diff) { concatPrimes += permutationPrimes[k].ToString(); if (givenSolution != concatPrimes) { Console.WriteLine(); return(concatPrimes); } } } } } } Console.WriteLine(); return(givenSolution); }
public void MatchingPermutationsTest() { char[] permutable = new char[] { '1', '2', '3' }; IEnumerable <char[]> expectedMatches = new List <char[]> { new char[] { '1', '2', '3' }, new char[] { '3', '2', '1' } }; var permutations = Permutations.Generate(permutable); var matchingPermutations = Permutations.GetMatches(permutable, permutations); Assert.IsTrue(matchingPermutations.SelectMany(x => x).SequenceEqual(expectedMatches.SelectMany(x => x))); }
private string FindNthOrderedPermutation(PermutationInput input, bool logging) { List <string> permutationMembers = input.PermutationString.Select(item => item.ToString()).ToList(); List <string> permutations = new List <string> (); Permutations.Generate("", permutationMembers, permutations); foreach (var permutation in permutations) { if (logging) { Console.WriteLine(permutation); } } return(permutations.Count() > input.PermutationIndex ? permutations[input.PermutationIndex - 1] : "IndexError"); }
public void GeneratedPermutationsTest() { char[] permutable = new char[] { '1', '2', '3' }; IEnumerable <char[]> expectedPermutations = new List <char[]> { new char[] { '1', '2', '3' }, new char[] { '2', '1', '3' }, new char[] { '3', '1', '2' }, new char[] { '1', '3', '2' }, new char[] { '2', '3', '1' }, new char[] { '3', '2', '1' } }; var permutations = Permutations.Generate(permutable); Assert.IsTrue(permutations.SelectMany(x => x).SequenceEqual(expectedPermutations.SelectMany(p => p))); }
public override object Run(RunModes runMode, object input, bool Logging) { var multPermutation = (int)input; var start = 1; while (true) { if (start.ToString().Length != (start * multPermutation).ToString().Length) { start = (int)Math.Pow(10, start.ToString().Length); continue; } if (!start.ToString().Any(c => c == '5' || c == '0')) { start++; continue; } if (!Permutations.UniqueDigits(start)) { start++; continue; } var permutations = Permutations.Generate(start); var missingMultiple = false; for (int i = 1; i <= multPermutation; i++) { if (!permutations.Contains(start * i)) { missingMultiple = true; break; } } if (!missingMultiple) { return(start); } start++; } }
public void Generate_Numbers_4digits() { var permutations = Permutations.Generate(1234); Assert.AreEqual(24, permutations.Count); }