xxxxxxxxxx
40
function* collatz(start) {
while (start > 1) {
yield start;
start = start % 2 == 0 ? start / 2 : (start * 3) + 1
}
}
let c;
let v;
function setup() {
createCanvas(400, 400);
background(51);
v = floor(random(1, 100000));
console.log(v);
c = collatz(v)
}
let values = []
function draw() {
textSize(24);
text(String(v), 50, 100);
let n = c.next();
if (n.value) {
values.push(n.value);
let max = values.reduce((acc, val) => acc > val ? acc : val);
fill(255);
for (let value of values) {
circle(width/2, map(value, 1, max, width-10, 10), 10);
}
} else {
text("Done", 50, 200)
}
}