P
Pascal
hello all
I try to find a class able to store in an array or a collection or something
like that (I don't know what will be the best practice) all the different
combinations of the partitions of any integer in four terms :
exemple :
8= (5 + 1 + 1 + 1)
= ( 4 + 2 + 1 + 1)
= ( 3 + 2 + 2 + 1 )
= ( 3 + 3 + 1 +1) etc.
( 4 + 2 + 1 + 1) is not different of ( 2 + 1 + 4 + 1)
I found a code on the net but in java :
http://bytes.com/topic/c/answers/869529-print-all-combinations-number-n-sum-positive-integers
Does someone knows something in java and vb, so he could translate it for
me.... I am not very handy in programmation, just make some stuff for my
young pupils.
Thanks in advance
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+" ");
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= k;
partition(p, n-k, n-k, i+1);
}
}
public static void main(String[] args) {
partition(new int[6], 6, 6, 0);
}
}
--
I try to find a class able to store in an array or a collection or something
like that (I don't know what will be the best practice) all the different
combinations of the partitions of any integer in four terms :
exemple :
8= (5 + 1 + 1 + 1)
= ( 4 + 2 + 1 + 1)
= ( 3 + 2 + 2 + 1 )
= ( 3 + 3 + 1 +1) etc.
( 4 + 2 + 1 + 1) is not different of ( 2 + 1 + 4 + 1)
I found a code on the net but in java :
http://bytes.com/topic/c/answers/869529-print-all-combinations-number-n-sum-positive-integers
Does someone knows something in java and vb, so he could translate it for
me.... I am not very handy in programmation, just make some stuff for my
young pupils.
Thanks in advance
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+" ");
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= k;
partition(p, n-k, n-k, i+1);
}
}
public static void main(String[] args) {
partition(new int[6], 6, 6, 0);
}
}
--