how to find all the partitions of any integer in four terms?

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);
}
}
--
 
V

vanderghast

Probably through LINQ (object, or SQL), or plain SQL. The basic idea is to
have a "table" iotas, one field, iota, with values from 1 to
ceiling(number/4). Then, generates the equivalent SQL statement:


SELECT a, b, c, d
FROM iotas AS a INNER JOIN iotas AS b
ON a.iota >= b.iota
INNER JOIN iotas AS b
ON b.iota >=c.iota
INNER JOIN iotas AS d
ON c.iota >= d.iota
AND a.iota+b.iota+c.iota+d.iota = Number
ORDER BY a, b, c


Note that the order by clause is facultative.



Vanderghast, Access MVP
 
V

vanderghast

Probably through LINQ (object, or SQL), or plain SQL. The basic idea is to
have a "table" iotas, one field, iota, with values from 1 to
ceiling(number/4). Then, generates the equivalent SQL statement:


SELECT a, b, c, d
FROM iotas AS a INNER JOIN iotas AS b
ON a.iota >= b.iota
INNER JOIN iotas AS b
ON b.iota >=c.iota
INNER JOIN iotas AS d
ON c.iota >= d.iota
AND a.iota+b.iota+c.iota+d.iota = Number
ORDER BY a, b, c


Note that the order by clause is facultative.



Vanderghast, Access MVP
 
P

Pascal

Thanks a lot for reply, I have posted that here because on the vb forum they
told me it is C# code.
You can consider the "System.out" as roughly equivalent to the Console
class, with the Write() and WriteLine() methods accomplishing basically
the same thing as print() and println().
With that information, i will dig to find my way.
Isn't it even that much more important that the teacher be doing their own
homework?
thanks! for helping me in my "homehobby" I have enough homework.... trying
to understand how to program an idea (even if that idea is in touch with my
job) is rather an escape to something else.

pascal

--
http://www.scalpa.info
http://scalpa98.blogspot.com/
http://scalpa-production.blogspot.com/
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 :
[...]
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.

Your pupils?

Hmm...well, I know enough to not do _student_ homework for them. But I'm
trying to figure out how to apply the same reasoning to the _teacher's_
homework. Isn't it even that much more important that the teacher be
doing their own homework?

Anyway, as far as your question goes, this is a C# newsgroup, so I don't
know why you are asking about VB. With respect to the Java code, it's not
really clear to me what the question is. C# and Java have practically the
same syntax in a lot of ways, and this is especially true for the code you
posted. The main difference is in the framework API. You can consider
the "System.out" as roughly equivalent to the Console class, with the
Write() and WriteLine() methods accomplishing basically the same thing as
print() and println().

With that information, you should easily be able to translate the code
yourself, inasmuch as any translation is really needed at all. ;)

Pete
 
P

Pascal

Thanks a lot for reply, I have posted that here because on the vb forum they
told me it is C# code.
You can consider the "System.out" as roughly equivalent to the Console
class, with the Write() and WriteLine() methods accomplishing basically
the same thing as print() and println().
With that information, i will dig to find my way.
Isn't it even that much more important that the teacher be doing their own
homework?
thanks! for helping me in my "homehobby" I have enough homework.... trying
to understand how to program an idea (even if that idea is in touch with my
job) is rather an escape to something else.

pascal

--
http://www.scalpa.info
http://scalpa98.blogspot.com/
http://scalpa-production.blogspot.com/
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 :
[...]
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.

Your pupils?

Hmm...well, I know enough to not do _student_ homework for them. But I'm
trying to figure out how to apply the same reasoning to the _teacher's_
homework. Isn't it even that much more important that the teacher be
doing their own homework?

Anyway, as far as your question goes, this is a C# newsgroup, so I don't
know why you are asking about VB. With respect to the Java code, it's not
really clear to me what the question is. C# and Java have practically the
same syntax in a lot of ways, and this is especially true for the code you
posted. The main difference is in the framework API. You can consider
the "System.out" as roughly equivalent to the Console class, with the
Write() and WriteLine() methods accomplishing basically the same thing as
print() and println().

With that information, you should easily be able to translate the code
yourself, inasmuch as any translation is really needed at all. ;)

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top