Prime disk 📀

Numberspiral.com primes to 100000
Every prime number is plotted with both angle and radius to the root of itself

Log in to post a comment.

// You can find the Turtle API reference here: https://turtletoy.net/syntax
Canvas.setpenopacity(-.7);

// Global code will be evaluated once.
const turtle = new Turtle();

// The walk function will be called until it returns false.
function walk(i) {
    if(isPrime(i)) [[
        .3 * i**.5 * Math.cos(i**.5 * 2 * Math.PI),
        .3 * i**.5 * Math.sin(i**.5 * 2 * Math.PI),
    ]].forEach(pt => [.2, .4].forEach(r => drawPoint(turtle, pt, r)));
    return i < 100000;
}

const isPrime = num => {
    for(let i = 2, s = Math.sqrt(num); i <= s; i++) if(num % i === 0) return false;
    return num > 1;
}

function add2(a, b) { return [a[0]+b[0], a[1]+b[1]]; }

////////////////////////////////////////////////////////////////
// Start of some path utility code - Created by Jurgen Westerhof 2023
////////////////////////////////////////////////////////////////
function circlePoints(radius, extend = 2 * Math.PI, clockWiseStart = 0, steps = null, includeLast = false) { return [steps == null? (radius*extend+1)|0: steps].map(steps => Array.from({length: steps}).map((v, i, a) => [radius * Math.cos(clockWiseStart + extend*i/(a.length-(includeLast?1:0))), radius * Math.sin(clockWiseStart + extend*i/(a.length-(includeLast?1:0)))])).pop(); }
function pts2Edges(pts) { return pts.map((v, i, a) => [v, a[(i+1)%a.length]]); }
function drawPath(turtle, pts) { return pts.forEach((pt, i) => turtle[i == 0? 'jump':'goto'](pt)); }
function drawTour(turtle, pts) { return drawPath(turtle, pts.concat([pts[0]])); }
function drawPoint(turtle, pt, r = .5) { return drawTour(turtle, circlePoints(r).map(p => add2(p, pt))); }