I'm struggling to make this code work properly. The goal is to create fibonacci codes for values 0-127 to be used later in a program intended to compress a text file.
Examples
0: 1
1: 11
2: 011
3: 0011
4: 1011
5: 00011
6: 10011
for {1, 2, 3, 5, 8, 13...} with an extra 1 at the end to make it clear when the specific char is completed.
Any help would be appreciated, I've been playing with it for a while now trying to make it work, so if anything isn't clear ask.
If you have a better algorithm for this situation, that would be great as well if you could lead me in the right direction.
public static void makeFibCodes (String[] fibCodes) {
int[] fibSeq = {1,2,3,5,8,13,21,34,55,89,144};
String s = "1";
for (int i = 1; i <= 127; i++) {
s = "1";
s = makeFibRec(fibSeq, i, s, false, 9);
fibCodes[i] = s;
System.out.println(s);
}
}
public static String makeFibRec (int[] fibSeq, int i, String s, boolean hasValue, int fibI) {
if (i == 0) {
hasValue = true;
return "0" + s ;
} else if (i == 1) {
hasValue = true;
return "1" + s;
} else {
for (int j = fibI; j > 0; j--) {
if (i > fibSeq[j]) {
hasValue = true;
s = "1" + s;
i = i - fibSeq[j];
return makeFibRec(fibSeq, i, s, hasValue, j) + s;
} if (hasValue) {
s = "0" + s;
}
}
return s;
}
}