Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
30
Task/Partition-function-P/Python/partition-function-p-3.py
Normal file
30
Task/Partition-function-P/Python/partition-function-p-3.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
from typing import List
|
||||
|
||||
|
||||
def partitions(n: int) -> int:
|
||||
"""Count partitions."""
|
||||
p: List[int] = [1] + [0] * n
|
||||
for i in range(1, n + 1):
|
||||
k: int = 0
|
||||
while True:
|
||||
k += 1
|
||||
j: int = (k * (3*k - 1)) // 2
|
||||
if (j > i):
|
||||
break
|
||||
if (k & 1):
|
||||
p[i] += p[i - j]
|
||||
else:
|
||||
p[i] -= p[i - j]
|
||||
j = (k * (3*k + 1)) // 2
|
||||
if (j > i):
|
||||
break
|
||||
if (k & 1):
|
||||
p[i] += p[i - j]
|
||||
else:
|
||||
p[i] -= p[i - j]
|
||||
|
||||
return p[n]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("\nPartitions:", [partitions(x) for x in range(15)])
|
||||
Loading…
Add table
Add a link
Reference in a new issue