using Common.Math; using System; using System.Collections.Generic; namespace ProjectEuler_24 { internal class Program { private static void Main(string[] args) { ListFactorial CodepermNumbers = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[] answer = new int[10]; // the number of permutations remaining is 1 less than the number we are // looking for because the first perumation is our starting point int permutationsRemaining = 999999; int answerIdx = 0; for (int i = 1; i < 10; i++) { // because the most permutations possible is described as n! // we want to figure out how many times we have to go through, // this gives us the first number int factorialVal = (10 - i).Factorial(); int x = permutationsRemaining / factorialVal; // now we need to calculate how many permutations remain after // we set the first number to continue narrowing down our calculation permutationsRemaining = permutationsRemaining % factorialVal; // set the answer then increment the index answer[answerIdx++] = permNumbers[x]; // can't use the number again, remove it permNumbers.RemoveAt(x); // make sure we haven't already calculated the permuation value // we're trying to get if (permutationsRemaining == 0) { break; } } Console.WriteLine($"The 1000000th lexicographic permutation is: {string.Join("", answer)}"); } } }
public static int Factorial(this int num) { if (num < 0) { return 0; } int result = 1; for (int i = 1; i <= num; i++) { result *= i; } return result; }
My spouse and I stumbled over here from a different page and thought I may as well check things out. I like what I see so i amm just following you. Look forward to looking over your web page repeatedly. https://Zeleniymis.Com.ua/
https://Zeleniymis.Com.ua/ August 4, 2024 3:14:57 AM