User blog comment:VoidSansXD/My attempt at not creating a recursive function/@comment-35470197-20190209232957/@comment-32994025-20190213165717

function isPrime(n){ if (n%2===0) return false; for (var f=3;f<=Math.sqrt(n);f+=2) if (n%f===0) return false; return true; } function J(n){ var have=n; var eaten=0; var steps=0; while (have!=eaten){ if (isPrime(have)){ have--; eaten++; }else have++; steps++; }  return steps; }