Skip to content

Instantly share code, notes, and snippets.

@risentveber
Created February 17, 2018 13:26
Show Gist options
  • Save risentveber/6fcfc55956b9320d4cd0e3038ed8c405 to your computer and use it in GitHub Desktop.
Save risentveber/6fcfc55956b9320d4cd0e3038ed8c405 to your computer and use it in GitHub Desktop.
import itertools
import math
import sys
count = 0
n = int(sys.argv[1])
for left in itertools.permutations(range(1, n + 1)):
for right in itertools.permutations(range(1, n + 1)):
rIsGood = all(i + 1 != r for i, r in enumerate(right))
lIsGood = all(i + 1 != l for i, l in enumerate(left))
if rIsGood or lIsGood:
print left, right
count += 1
print count, math.factorial(n)**2
print count / float(math.factorial(n)**2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment