In order to apply constraints while generating permutations, I needed a new algorithm. Haskell’s library function Data.List.permutations builds permutations using interleave, so each item added could end up anywhere in the list. I needed items to stay in the same place once chosen. Note that this performs much worse than Data.List.permutations, because it has toContinue reading “Permutations with constraints”
Author Archives: alistairjwall
Magic Square by brute force
This was inspired by Tsoder’s video https://www.youtube.com/watch?v=Qf4NyHiy0W8 where he generated all the 3×3 magic squares by brute force, generating all permutations of 1-9 and rejecting the ones which were not magic squares. My first reaction was that that was not brute force; this was brute force: This turned out to be faster than Tsoder’s method,Continue reading “Magic Square by brute force”
FizzBuzz
FizzBuzz is a cut-down version of Eratosthenes’ sieve, which started as a drinking game and is now often used as an interview question. The rules are that you start counting, and replace numbers by Fizz if they are divisible by three, Buzz if they are divisible by five, and FizzBuzz if divisible by both. TheContinue reading “FizzBuzz”