# Help for lexicographic premutations

I kinda have a solution in mind for this and it goes like so:
there are a total of 10! permutations (10 digits total), the first 9! (from 1 - 9!) solutions contain 0 as the first digit, second 9! (9! - 9!*2) contain 1 as the first and so on until 9!*10 which contains 9 as the first digit, when u find the range of the first digit you know you will have now 9! options, first 9! will contain 0 (or 1 if you have 0 as the first digit), second 9! (9!-9!*2) will contain 1(or 0 if you have 1 as the first digit) and you reapeat this procces until 2! = 2 (then you will have only one option), how do you bring this idea into (fast running if possible) code?

Edit: i meant 8! in the second digit, idea is to decrement the factorial every digit you go

what do you want as output? all the possible permutations?

if that the case you can google for the algorithm, you can easily find it and implement it in your language of choice

I want to output the n-th lexicograpthic permutation, and I don’t wanna google it cuz I don’t want to see solutions but rather actually do it myself (with some hints from here)

are you really sure that’s what you want?
a permutation algorithm is something matemathics try to invent

if you want to invent one yourself I would have no idea what hints to give

wikipedia has a lexographic order permutation algorithm in the Permutation article. It’s not a solution, you still have to do all the work of implementing it in javascript.

sorry i didn’t provide the link, my bad, anyway, it’s project euler 24, here it is https://www.freecodecamp.org/learn/coding-interview-prep/project-euler/problem-24-lexicographic-permutations

there is any reason for which you don’t want to use an alreay existing algorithm?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.