Partition function P: Difference between revisions

(→‎{{header|jq}}: header - first step to workaround RC bugs)
Line 587:
 
=={{header|jq}}==
 
Translation of: Python:Alternative
 
<lang jq>def partitions($n):
def div2: (. - (.%2)) / 2;
reduce range(1; $n + 1) as $i ( {p: ([1] + [range(0;$n)|0])};
. + {k: 0, stop: false}
| until(.stop;
.k += 1
| (((.k * (3*.k - 1)) | div2) ) as $j
| if $j > $i then .stop=true
else if (.k % 2) == 1
then .p[$i] = .p[$i] + .p[$i - $j]
else .p[$i] = .p[$i] - .p[$i - $j]
end
| (((.k * (3*.k + 1)) | div2)) as $j
| if $j > $i then .stop=true
elif (.k % 2) == 1
then .p[$i] = .p[$i] + .p[$i - $j]
else .p[$i] = .p[$i] - .p[$i - $j]
end
end ))
| .p[$n] ;
 
[partitions(range(1;15))]</lang>
{{out}}
<pre>[1,2,3,5,7,11,15,22,30,42,56,77,101,135]</pre>
 
jq's built-in integer precision is insufficient for computing ``partitions(6666)``, but more as a test
of the BigInt.jq library for jq than anything else, here are the results of using it in conjunction
with a trivially-modified version of the ''partitions'' implementation above. That is, after
modifying the lines that refer to "p" (or ".p"), we see that ''partitions(6666)'' yields:
 
"193655306161707661080005073394486091998480950338405932486880600467114423441282418165863"
 
The user+sys time is 7m3s as the BigInt.jq library is written in jq.
 
=={{header|Julia}}==
2,442

edits