Not the Recamán’s sequence

I asked Chat GPT write a turtletoy program to generate the Recamán’s sequence.
Although it claims that this is correct it's not.
But is is something. It did take a little debugging to get it to work.

Log in to post a comment.


// Global code will be evaluated once.
const turtle = new Turtle();
turtle.penup();
turtle.goto(-90,0);
turtle.pendown();
turtle.left(90);

// Parameters
let steps = 55; // min=1 max=90 step=1
let scale = 2.5;   // min=1 max=10 step=0.1

let visited = new Set();
let a = 0;
visited.add(a);

// Start position
turtle.penup();
turtle.forward(0);
turtle.pendown();

for (let n = 1; n <= steps; n++) {
  let candidate = a - n;
  let next;

  if (candidate > 0 && !visited.has(candidate)) {
    next = candidate;
  } else {
    next = a + n;
  }

  // Distance between points
  let d = Math.abs(next - a) * scale;
  let radius = d / 2;

  // Draw semicircle
  drawArc(radius, 180, n % 2 === 0 ? 1 : -1);

  a = next;
  visited.add(a);
}

// ---- Helper function ----
function drawArc(radius, degrees, direction) {
  let steps = 60;
  let stepAngle = degrees / steps;
  let stepLength = (Math.PI * radius * degrees / 180) / steps;

  turtle.left(direction * 90);
  for (let i = 0; i < steps; i++) {
    turtle.forward(stepLength);
    turtle.right(direction * stepAngle);
  }
  turtle.right(direction * 90);
}