Problem 55280. Count estrangements

Recently I made a puzzle for my wife that included a cryptogram, which involves an arrangement of the letters A through Z. I used MATLAB (of course) to permute the letters, but in the first arrangement, H was coded as H. I tried again until MATLAB gave me a derangement, a permutation such that none of the letters was in its original position. I later noticed that the letter I was coded as S and S was coded as I. Though I used that permutation, I really wanted one in which no two elements are simply swapped. I called such a permutation an estrangement, and although I later learned of a more technical and mathematical description, I will keep my name.
Write a function to count estrangements—i.e., the permutations of elements in a 1xn vector such that (1) no element is in its original position and (2) no two elements are simply swapped. For example, if the vector is [1 2 3 4], then [3 2 4 1] and [4 1 3 2] would not be allowed under condition 1 and [4 3 2 1] and [2 1 4 3] would not be allowed under condition 2. Return the count as a string.

Solution Stats

38.89% Correct | 61.11% Incorrect
Last Solution submitted on Jun 23, 2024

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers7

Suggested Problems

More from this Author279

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!