Created
January 8, 2020 16:47
-
-
Save nuclearace/edd8a15c947862ccbda598896ecac12f to your computer and use it in GitHub Desktop.
superpermutations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let max = 15 | |
var len = 0 | |
var counts = [Int](repeating: 0, count: max) | |
var pos = 0 | |
func factSum(n: Int) -> Int { | |
var s = 0 | |
var x = 0 | |
var f = 1 | |
while x < n { | |
x += 1 | |
f *= x | |
s += f | |
} | |
return s | |
} | |
func r(n: Int) -> Bool { | |
guard n != 0 else { | |
return false | |
} | |
counts[n] -= 1 | |
if counts[n] == 0 { | |
counts[n] = n | |
if !r(n: n - 1) { | |
return false | |
} | |
} | |
pos += 1 | |
return true | |
} | |
func superPermutations(n: Int) { | |
guard n != 0 else { | |
return | |
} | |
pos = n | |
len = factSum(n: n) | |
for i in 0...n { | |
counts[i] = i | |
} | |
while r(n: n) {} | |
} | |
for i in 0..<max { | |
superPermutations(n: i) | |
print("superPerm(\(i)) = \(len)") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment