Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Partition-function-P/Wren/partition-function-p.wren
Normal file
36
Task/Partition-function-P/Wren/partition-function-p.wren
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import "/big" for BigInt
|
||||
|
||||
var p = []
|
||||
var pd = []
|
||||
|
||||
var partDiffDiff = Fn.new { |n| (n&1 == 1) ? (n + 1)/2 : n + 1 }
|
||||
|
||||
var partDiff = Fn.new { |n|
|
||||
if (n < 2) return 1
|
||||
pd[n] = pd[n-1] + partDiffDiff.call(n-1)
|
||||
return pd[n]
|
||||
}
|
||||
|
||||
var partitionsP = Fn.new { |n|
|
||||
if (n < 2) return
|
||||
var psum = BigInt.zero
|
||||
for (i in 1..n) {
|
||||
var pdi = partDiff.call(i)
|
||||
if (pdi > n) break
|
||||
var sign = (i-1)%4 < 2 ? 1 : -1
|
||||
psum = psum + p[n-pdi] * sign
|
||||
}
|
||||
p[n] = psum
|
||||
}
|
||||
|
||||
var start = System.clock
|
||||
var N = 6666
|
||||
p = List.filled(N+1, null)
|
||||
pd = List.filled(N+1, 0)
|
||||
p[0] = BigInt.one
|
||||
p[1] = BigInt.one
|
||||
pd[0] = 1
|
||||
pd[1] = 1
|
||||
for (n in 2..N) partitionsP.call(n)
|
||||
System.print("p[%(N)] = %(p[N])")
|
||||
System.print("Took %(System.clock - start) seconds")
|
||||
Loading…
Add table
Add a link
Reference in a new issue