Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Partition-function-P/Jq/partition-function-p-1.jq
Normal file
22
Task/Partition-function-P/Jq/partition-function-p-1.jq
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
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))]
|
||||
49
Task/Partition-function-P/Jq/partition-function-p-2.jq
Normal file
49
Task/Partition-function-P/Jq/partition-function-p-2.jq
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
def partDiffDiff($n):
|
||||
if ($n % 2) == 1 then ($n+1) / 2 else $n+1 end;
|
||||
|
||||
# in: {n, partDiffMemo}
|
||||
# out: object with possibly updated memoization
|
||||
def partDiff:
|
||||
.n as $n
|
||||
| if .partDiffMemo[$n] then .
|
||||
elif $n<2 then .partDiffMemo[$n]=1
|
||||
else ((.n=($n-1)) | partDiff)
|
||||
| .partDiffMemo[$n] = .partDiffMemo[$n-1] + partDiffDiff($n-1)
|
||||
end;
|
||||
|
||||
# in: {n, memo, partDiffMemo}
|
||||
# where `.memo[i]` memoizes partitions(i)
|
||||
# and `.partDiffMemo[i]` memoizes partDiff(i)
|
||||
# out: object with possibly updated memoization
|
||||
def partitionsM:
|
||||
.n as $n
|
||||
| if .memo[$n] then .
|
||||
elif $n<2 then .memo[$n] = 1
|
||||
else label $out
|
||||
| foreach range(1; $n+2) as $i (.emit = false | .psum = 0;
|
||||
if $i > $n then .emit = true
|
||||
else ((.n = $i) | partDiff)
|
||||
| .partDiffMemo[$i] as $pd
|
||||
| if $pd > $n then .emit=true, break $out
|
||||
else {psum, emit} as $local # for restoring relevant state
|
||||
| ((.n = ($n-$pd)) | partitionsM)
|
||||
| .memo[$n-$pd] as $increment
|
||||
| . + $local # restore
|
||||
| if (($i-1)%4)<2
|
||||
then .psum += $increment
|
||||
else .psum -= $increment
|
||||
end
|
||||
end
|
||||
end;
|
||||
select(.emit) )
|
||||
| .memo[$n] = .psum
|
||||
end ;
|
||||
|
||||
def partitionsP:
|
||||
. as $n
|
||||
| {n: $n, memo:[], partDiffMemo:[]}
|
||||
| partitionsM
|
||||
| .memo[$n];
|
||||
|
||||
# Stretch goal:
|
||||
6666 | partitionsP
|
||||
Loading…
Add table
Add a link
Reference in a new issue