24 lines
488 B
Plaintext
24 lines
488 B
Plaintext
func partitionsP(n) {
|
|
func (n) is cached {
|
|
|
|
n <= 1 && return n
|
|
|
|
var a = sum(1..floor((sqrt(24*n + 1) + 1)/6), {|k|
|
|
(-1)**(k-1) * __FUNC__(n - ((k*(3*k - 1)) >> 1))
|
|
})
|
|
|
|
var b = sum(1..ceil((sqrt(24*n + 1) - 7)/6), {|k|
|
|
(-1)**(k-1) * __FUNC__(n - ((k*(3*k + 1)) >> 1))
|
|
})
|
|
|
|
a + b
|
|
}(n+1)
|
|
}
|
|
|
|
var t = Time.micro
|
|
|
|
say partitionsP.map(0..25).join(' ')
|
|
say partitionsP(6666)
|
|
|
|
say ("Took %.4f seconds" % Time.micro-t)
|