// You can find the Turtle API reference here: https://turtletoy.net/syntax
const iterations = 100; //min=1 max=1000 step=1
const scale=1.9; //min=.2 max=30 step=.1
const offsetX=-91; //min=-200 max=200 step=1
const offsetY=94; //min=-200 max=200 step=1
const spin=180; //min=0 max=359 step=1

Canvas.setpenopacity(1);

// Global code will be evaluated once.
const turtle = new Turtle();
turtle.jump(offsetX, offsetY);
turtle.seth(225);

const recamanIterator = recaman();

let lastValue = recamanIterator.next().value;

// The walk function will be called until it returns false.
function walk(i) {
    const thisValue = recamanIterator.next().value;
    const diff = thisValue - lastValue;

    turtle.circle(diff * (i%2==0?1:-1) / scale, spin);
    
    lastValue = thisValue;
    return i < iterations - 1;
}


function* recaman() {
    const sequence = [0];
    for(let i = 1; true; i++) {
        yield sequence[i - 1];
        [sequence[i-1]-i].forEach(next => sequence.push(next > 0 && !sequence.includes(next)? next: sequence[i-1]+i));
    }
}