Partition function P: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added HTML tags.)
(Added Wren)
Line 200: Line 200:
789845457356081278451246751375974038318319810923102769109446980055567090089060954748065131666952
789845457356081278451246751375974038318319810923102769109446980055567090089060954748065131666952
830623286286824837240058805177319865230913417587625830803675380262765598632742903192085254824621
830623286286824837240058805177319865230913417587625830803675380262765598632742903192085254824621
</pre>

=={{header|Wren}}==
{{trans|Julia}}
{{libheader|Wren-big}}
Although it may not look like it, this is actually a decent time for Wren which is interpreted and the above module is written entirely in Wren itself.
<lang ecmascript>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")</lang>

{{out}}
<pre>
p[6666] = 193655306161707661080005073394486091998480950338405932486880600467114423441282418165863
Took 1.428762 seconds
</pre>
</pre>