Re: partition numbers in four terms

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi there,

public class Partition {

private static void printPartition(int[] p, int n) {
if (n != 4) return; // bail out
for (int i= 0; i < n; i++)
System.out.print(p[i]+" ");
System.out.println();
}

private static void partition(int[] p, int n, int m, int i) {

if (n == 0)
printPartition(p, i);
else
for (int k= m; k > 0; k--) {
p[i]= k;
partition(p, n-k, n-k, i+1);
}
}

public static void main(String[] args) {

partition(new int[6], 6, 6, 0);
}
}

You want this code in VB.NET right? This is all untested and written in here so might have a couple of errors...

----

private shared sub printPartition(Byval p() as Integer, Byval n as Integer)
if(n <> 4) then return 'bail out
dim i as integer
for i = 0 to n-1 step 1
console.write(p(i).ToString() & " ")
next
console.writeline(String.empty)
end sub

private shared sub partition(Byval p() as Integer, Byval n as integer, Byval m as integer, Byval i as integer)
if(n = 0) then
printPartition(p,i)
else
Dim k as integer
for k = m to 0 step -1
p(i) = k
partition(p, n-k, n-k, i+1)
next
endif
end sub

public shared sub main(Byval args() as string)
dim pop[6] as integer
partition(pop,6,6,0)
end sub

----

Bit of a weird function though really as every time partition is called the same value is used for parameter 'n' and 'm', so why not just combine those into 1 parameter or does this do more?

Anyways, I hope this helps.

Nick.

.



Relevant Pages