One time, many years ago during my first pass through Project Euler, this problem owned the shit out of me. Like, badly owned me. For the life of me I couldn't make the mental translation from mathematical method to functional algorithm. I gave up in the end, and used a solution scalped from some random website.
Well, fuck that noise.
This is a fully-functional functional lexicographic permutation generator generator. It doesn't use some fancy algorithm or mathematical inside to jump to the solution of the exercise. No, fuck that. This code owns the shit out of the entire problem of lexicographic permutation in an over-engineered way. There are test suites and comment blocks and examples and recursive README files full of foul-mouthed trash talk.
npm install lexicographer
npm run test
(This also solves problem 24.)
#!/usr/bin/env nodeconst lexicographer = ;const array = ...Array10;const breakpoint = 1000000;let permutations = 0;for let value of lexicographerpermutations++;if permutations === breakpointconsole;break;