tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
|
|
@ -0,0 +1,9 @@
|
|||
def fact = { n -> [1,(1..<(n+1)).inject(1) { prod, i -> prod * i }].max() }
|
||||
def subfact
|
||||
subfact = { BigInteger n -> (n == 0) ? 1 : (n == 1) ? 0 : ((n-1) * (subfact(n-1) + subfact(n-2))) }
|
||||
|
||||
def derangement = { List l ->
|
||||
def d = []
|
||||
l.eachPermutation { p -> if ([p,l].transpose().every{ it[0] != it[1] }) d << p }
|
||||
d
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
def d = derangement([1,2,3,4])
|
||||
assert d.size() == subfact(4)
|
||||
d.each { println it }
|
||||
|
||||
println """
|
||||
n # derangements subfactorial
|
||||
= ============== ============"""
|
||||
(0..9). each { n ->
|
||||
def dr = derangement((1..<(n+1)) as List)
|
||||
def sf = subfact(n)
|
||||
printf('%1d %14d %12d\n', n, dr.size(), sf)
|
||||
}
|
||||
|
||||
println """
|
||||
!20 == ${subfact(20)}
|
||||
"""
|
||||
Loading…
Add table
Add a link
Reference in a new issue