partition numbers in four terms

F

Family Tree Mike

Pascal said:
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);
}
}


This is what I came up with:

Module Module1
Sub printPartition(ByVal p As Integer(), ByVal n As Integer)
If (n <> 3) Then Return
Dim i As Integer
For i = 0 To n
Console.Write("{0} ", p(i))
Next
Console.WriteLine()
End Sub

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 1 Step -1
p(i) = k
partition(p, n - k, n - k, i + 1)
Next
End If

End Sub

Sub Main()
Dim p() As Integer
ReDim p(6)

partition(p, 6, 6, 0)

Console.WriteLine("Done...")
Console.ReadLine()
End Sub

End Module
 
M

Michael Williams

Oh, you mean the same niche that VB6 filled....

Pretty much, except that VB.Net is more "dumbed down" than the real Visual
Basic ever was, making it difficult to explain the completely unfounded
elitist attitude of many VB.Net programmers :)
 
M

Michael Williams

Oh, you mean the same niche that VB6 filled....

Pretty much, except that VB.Net is more "dumbed down" than the real Visual
Basic ever was, making it difficult to explain the completely unfounded
elitist attitude of many VB.Net programmers :)
 
M

Michael Williams

Right, your perception does not make sense.

No, your statement does not make sense. Perhaps it has lost something in the
translation from your native language into English?

Mike
 
M

Michael Williams

Right, your perception does not make sense.

No, your statement does not make sense. Perhaps it has lost something in the
translation from your native language into English?

Mike
 
T

Tom Shelton

Oh, you mean the same niche that VB6 filled....

Pretty much, except that VB.Net is more "dumbed down" than the real Visual
Basic ever was, making it difficult to explain the completely unfounded
elitist attitude of many VB.Net programmers :)

Given the vastly technically superior nature of their tool - they have
the right to feel that way. I won't even comment on the "dumbed down"
comment, as it is so ludicrous as to be laughable.
 
T

Tom Shelton

Oh, you mean the same niche that VB6 filled....

Pretty much, except that VB.Net is more "dumbed down" than the real Visual
Basic ever was, making it difficult to explain the completely unfounded
elitist attitude of many VB.Net programmers :)

Given the vastly technically superior nature of their tool - they have
the right to feel that way. I won't even comment on the "dumbed down"
comment, as it is so ludicrous as to be laughable.
 
N

nak

Pretty much, except that VB.Net is more "dumbed down" than the real Visual
Basic ever was, making it difficult to explain the completely unfounded
elitist attitude of many VB.Net programmers :)

Yeah okay, you "elitist", or should I say "legacy dinosaur fossil coder".

Are you fools still going on about vb6 being better than vb.net? Give me a
break, you are talking garbage and need to learn a new language by the
sounds of things.

Get with the times.
 
N

nak

Pretty much, except that VB.Net is more "dumbed down" than the real Visual
Basic ever was, making it difficult to explain the completely unfounded
elitist attitude of many VB.Net programmers :)

Yeah okay, you "elitist", or should I say "legacy dinosaur fossil coder".

Are you fools still going on about vb6 being better than vb.net? Give me a
break, you are talking garbage and need to learn a new language by the
sounds of things.

Get with the times.
 
B

Branco

Cor Ligthert[MVP] wrote:
Your code is done in the program language C# here it is about VB
<snip>


The code is in Java and he is explicitly asking for an similar
solution in Vb.

Regards,

Branco.
 
B

Branco

Cor Ligthert[MVP] wrote:
Your code is done in the program language C# here it is about VB
<snip>


The code is in Java and he is explicitly asking for an similar
solution in Vb.

Regards,

Branco.
 
N

nak

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+" ");
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);
}
}


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.
 
N

nak

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+" ");
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);
}
}


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.
 
N

nak

That doesn't even make sense, Zingler. Perhaps its a Germanic thing? You
Germans certainly are a weird bunch. No real sense of humour at all. By
the way, I would probably recognise your wife, although not if she's
wearing a dress of course ;-)

hahaha
 
N

nak

That doesn't even make sense, Zingler. Perhaps its a Germanic thing? You
Germans certainly are a weird bunch. No real sense of humour at all. By
the way, I would probably recognise your wife, although not if she's
wearing a dress of course ;-)

hahaha
 
N

nak

Oops you got there first I didn't even notice! doh!
Sub Main()
Dim p() As Integer
ReDim p(6)

partition(p, 6, 6, 0)

Console.WriteLine("Done...")
Console.ReadLine()
End Sub

Although, why redim out of curiosity?
 

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