Thursday, April 17, 2008

Recursion sucks?

After writing yesterday's post I thought that many people don't like (I mean understand) recursion. Because of this I decided to modify my "algorithm" ;) and make it iterative.

Enjoy!



private static String convertIter(int decimal) {
StringBuilder sb = new StringBuilder();
int number = decimal;

while (number > 0) {
for (int i = 0; i < NUMBERS.length; i++) {
if (number >= NUMBERS[i]) {
sb.append(LETTERS[i]);
number -= NUMBERS[i];
break;
}
}
}

return sb.toString();
}

No comments: