Elegant Project Euler Solution

Kreikey rick.kreikebaum at gmail.com
Wed Nov 13 01:29:48 UTC 2019


Yeah, I know. I found it after I returned to this thread. It was 
a bit confusing away from my computer cause I thought the OP must 
have been using the permutations() function, and I was like "wait 
a minute, how can that  be correct when it doesn't generate them 
in lexicographical order." When I checked it again, I saw he used 
the nthPermutation() function and found it in the documentation. 
I'll file a bug report when I get around to it.

When I solved Problem 24 myself a couple years ago, I wrote my 
own spaghetti code algorithm for it, as follows:

Iterate backwards until you find the first item that's less than 
the previous
Find the lowest item after that that's still greater than it
Swap the two
Sort the portion of the array after the index of the item in step 
1

For some of the later problems, I just used the standard 
library's nextPermutation() function. But now that I looked at my 
old code again, I think I'm going to factor out my algorithm into 
my own nextPermutation function with the same semantics as the 
one in the standard library.

Fun stuff...


More information about the Digitalmars-d mailing list