Help me with this algoritm

Hello all,

Let us assume n = 2. I want to print like this:

0 0

0 1

1 0

1 1

Let us assume n = 3. I want to print like this:

0 0 0

0 0 1

0 1 0

0 1 1

1 0 0

1 0 1

1 1 0

1 1 1

In the same way I want to print for any value of "n". Is there any algorithm for this?

thanks and regards,

dakamath

[396 byte] By [dakamatha] at [2007-9-29 5:14:18]
# 1
Integer.toBinaryString(n) ?
ajoachima at 2007-7-14 18:26:42 > top of Java-index,Other Topics,Algorithms...
# 2
Thank you!
dakamatha at 2007-7-14 18:26:42 > top of Java-index,Other Topics,Algorithms...
# 3
> Integer.toBinaryString(n) ?A very complex algorithm indeed :)
amishslayera at 2007-7-14 18:26:42 > top of Java-index,Other Topics,Algorithms...
# 4

Yes, it is complex. But fortunately we have "Integer.toBinaryString" which serves my current immediate requirement!

Original requirement is something like this:

I have "n" variables, each having 2 different values. I need to have all possible combinations of these variables.

thanks,

dakamath

dakamatha at 2007-7-14 18:26:42 > top of Java-index,Other Topics,Algorithms...
# 5
The most complex implementation would probably shift n right and check if its odd or even at each step. Sligthly less complex than pulling out the API doc and find toBinaryString() :&)
ajoachima at 2007-7-14 18:26:42 > top of Java-index,Other Topics,Algorithms...
# 6

>In the same way I want to print for any value of "n". Is there any algorithm for this?

We all know the algorithm for this. We learned it in grade school.

Think about how a gradeschool student would add 999 + 3 on a piece of paper. 9+3 = 12, carry the 1. 9+1=10, carry the 1 ...

The only difference is you're adding in base 2. And you're always adding 1. Pretty easy.

To sum up:

1. Add value.

2. Carry exess into next column.

mgbolusma at 2007-7-14 18:26:42 > top of Java-index,Other Topics,Algorithms...
# 7
You obviously think what you said is simple to implement. Why don't you post your code for a binary adder and then compare it to the code I have posted...
rkippena at 2007-7-14 18:26:42 > top of Java-index,Other Topics,Algorithms...
# 8
http://forum.java.sun.com/thread.jsp?forum=426&thread=423657&tstart=0&trange=15
rkippena at 2007-7-14 18:26:42 > top of Java-index,Other Topics,Algorithms...
# 9

> You obviously think what you said is simple to

> implement.

Not trivial, but simple.

>Why don't you post your code for a binary

> adder and then compare it to the code I have posted...

Here is a general purpose base-n adder. So long as N<Integer.MAX_VALUE/3;

It's not tailored to be a solution to a specific problem. Just an example of how to implement such a thing.

For example, to iterate through binary values

Use base 2 and add {0, ... 0, 1} repeadedly.

Optimizations (such as caller supplying and reusing result array) are easy enough to do for a customized implementation.

public class Adder {

private int base;

public Adder(int base)

{

this.base = base;

}

public int[] add(int[] n1, int[] n2)

{

if (n1.length!=n2.length)

throw new IllegalArgumentException("n1 and n2 must be same length");

int[] result = new int[n1.length +1 ]; //result may need extra digit.

int carry = 0;

for (int i=result.length-1; i>=1; i--)

{

result[i] = carry;

//arrays are right aligned, "Most Significant Int" first (at 0)

//we must shift to account for the result being 1 larger

//hence the [i-1]

result[i]+=n1[i-1];//

result[i]+=n2[i-1];// Algorithm: add the values

//

carry = result[i]/base;// carry the extra

result[i]%=base;//

}

result[0] = carry;

return result;

}

}

mgbolusma at 2007-7-14 18:26:42 > top of Java-index,Other Topics,Algorithms...
# 10

Don't forget you need this code too!

public static void main(String[] args) {

Adder adder = new Adder(2);

int[] n1 = new int[4];

int[] n2 = new int[] { 0, 0, 0, 1};

for (int i = 0; i < Math.pow(2, n1.length); i++) {

for (int j = 0; j < n1.length; j++)

System.out.print(n1[j]);

System.out.println();

int[] result = adder.add(n1, n2);

for (int j = 0; j < n1.length; j++)

n1[j] = result[j+1];

}

}

rkippena at 2007-7-14 18:26:42 > top of Java-index,Other Topics,Algorithms...