Converting decimal number to ternary number

So here is my attempt (below). Sadly it does not work:
Void methods cannot return a value

I am trying to use a recursive function. Can it be done?

void setup() {
  noLoop();
}


void draw() {
  for (int i = 0; i < 34; i++) {
    String t = tern(i);
    Println(t);
  }
}


String tern(int dec) {
  float a = dec/3;
  int e = floor(a);

  int q = dec % 3;

  if (dec == 0) {
    return str(0);
  } else if (e == 0) {
    return str(q);
  } else {
    return tern(e) + str(q);
  }
}

I’m not getting that error you are reporting.
Though there is an issue with Println(t); – an errant capital P. Correcting that runs the sketch with no problems and produces an output. Admittedly, I have not evaluated the results to check if your conversion is fully correct.

1 Like

Doh. That has solved it. Thank you.

The results look good to me, but…