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]

Integer.toBinaryString(n) ?
> Integer.toBinaryString(n) ?A very complex algorithm indeed :)
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
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() :&)
>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.
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...
http://forum.java.sun.com/thread.jsp?forum=426&thread=423657&tstart=0&trange=15
> 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;
}
}
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];
}
}