After correcting mistakes, we have algorithm implementation for counting Factor321 (Representation of integer number using 3,2 and 1 numbers with operations of power, multiplication, addition) of any positive integer number that can be represented in computer memory (storage). It has it's uses in data compression and data flow efficiency.

Click 'more' to see Source Code:

Source code:

package factor321;

public class Factor321 {

private int numberToFactor;

private int factoredNumber;

private int threes;

private int twos;

private Factor321 tail;

private void separate3() {

if (factoredNumber >= 3) {

threes++;

factoredNumber = factoredNumber / 3;

separate2();

}

}

private void separate2() {

if (factoredNumber >= 2) {

twos++;

factoredNumber = factoredNumber / 2;

separate3();

}

}

public Factor321(int numberToFactor) {

this.numberToFactor = numberToFactor;

this.factoredNumber = numberToFactor;

}

private void executeFactoring() {

separate3();

int partialResult = (int) Math.pow(3, threes) * (int) Math.pow(2, twos);

if (partialResult != numberToFactor) {

tail = new Factor321(numberToFactor - partialResult);

tail.executeFactoring();

}

}

public String toString() {

StringBuilder sb = new StringBuilder();

if (threes == 0 && twos == 0) {

sb.append("1");

} else {

sb.append("3^");

sb.append(threes);

sb.append("*2^");

sb.append(twos);

}

if (tail != null) {

sb.append("+");

sb.append(tail.toString());

}

return sb.toString();

}

public static void main(String[] args) {

int numberToFactor = 108;

Factor321 factor321 = new Factor321(numberToFactor);

factor321.executeFactoring();

System.out.println(numberToFactor + " can be represented as : "

+ factor321.toString());

}

}

Alternate solution would be to compute result of equation written in matrix form.

ReplyDelete