How to resolve the algorithm Partition function P step by step in the Julia programming language
How to resolve the algorithm Partition function P step by step in the Julia programming language
Table of Contents
Problem Statement
The Partition Function P is the function P(n), where n∈ℤ, defined as the number of distinct ways in which n can be expressed as the sum of non-increasing positive integers.
P(n) can be expressed as the recurrence relation: The successive numbers in the above equation have the differences: 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15, 8 ... This task may be of popular interest because Mathologer made the video, The hardest "What comes next?" (Euler's pentagonal formula), where he asks the programmers among his viewers to calculate P(666). The video was viewed more than 100,000 times in the first couple of weeks after its release. In Wolfram Language, this function has been implemented as PartitionsP.
Write a function which returns the value of PartitionsP(n). Solutions can be iterative or recursive. Bonus task: show how long it takes to compute PartitionsP(6666).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Partition function P step by step in the Julia programming language
The provided code computes the partition function for a given integer n
, which represents the number of distinct ways in which a positive integer n
can be represented as a sum of positive integers. Here's a breakdown of the code:
-
Function
partDiffDiff(n)
: This function computes the difference between two consecutive partition numbers. It returns(n+1)÷2
ifn
is odd andn+1
ifn
is even. -
Function
partDiff(n)
with@memoize
: This function computes the partition numberp(n)
, which represents the number of ways to representn
as a sum of positive integers. The@memoize
decorator is used to cache the results of the function, so that subsequent calls with the same input will return the cached value. -
Function
partitionsP(n)
with@memoize
: This function computes the partition functionp(n)
using the pentagonal number theorem. It considers consecutive partitionsp(i)
and adds or subtracts them to the partial sum, depending on the value ofi
. The result is then returned as aBigInt
type. The@memoize
decorator is used here as well to cache the results. -
Main Code: In the main part of the code, the value of
n
is set to 6666. The@time
decorator is used to measure the execution time of thepartitionsP(n)
function. The result is then printed to the console.
Here are some of the key implementation details:
-
The function
partDiff
uses the recurrence relationp(n) = p(n-1) + p(n-2) + p(n-5) + ...
to compute the partition numberp(n)
. -
The function
partitionsP
uses the pentagonal number theorem, which states that if the pentagonal numbersP(k)
are defined asP(k) = k * (3 * k - 1) / 2
, thenp(n) = (-1)^k * P(k)
for oddk
such thatP(k) <= n
andp(n) = 0
otherwise.
This code provides a method for efficiently computing the partition function p(n)
for large values of n
. The memoization technique used in the partDiff
and partitionsP
functions significantly improves the performance of the computation.
Source code in the julia programming language
using Memoize
function partDiffDiff(n::Int)::Int
isodd(n) ? (n+1)÷2 : n+1
end
@memoize function partDiff(n::Int)::Int
n<2 ? 1 : partDiff(n-1)+partDiffDiff(n-1)
end
@memoize function partitionsP(n::Int)
T=BigInt
if n<2
one(T)
else
psum = zero(T)
for i ∈ 1:n
pd = partDiff(i)
if pd>n
break
end
if ((i-1)%4)<2
psum += partitionsP(n-pd)
else
psum -= partitionsP(n-pd)
end
end
psum
end
end
n=6666
@time println("p($n) = ", partitionsP(n))
You may also check:How to resolve the algorithm Empty program step by step in the Action! programming language
You may also check:How to resolve the algorithm Modular inverse step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Program termination step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Munching squares step by step in the Evaldraw programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the TUSCRIPT programming language