7 ms·
It had to be done: setInterval(function() { var n = parseInt(document.querySelector("#n").textContent); document.querySelector(is_prime(n) ===
by tlrobinson 11y ago
It had to be done:
setInterval(function() {
var n = parseInt(document.querySelector("#n").textContent);
document.querySelector(is_prime(n) === "prime" ? "#yes" : "#no").click();
}, 0);
document.querySelector("#start").click();
EDIT: replaced my isPrime with the one already in the game.
EDIT 2: There appears to be some sort of increasing delay (pretty sure it's not just `is_prime` being slow) starting around 500 points. Maybe the game itself trying to compute primes?
- WA 11y agoAh, you beat me to the minute (game comes with a function is_prime): var yes = document.getElementById('yes'), no = document.getElementById('no'), n = document.getElementById('n'), start = document.getElementById('start'); function tick() { console.log('tick'); if (is_prime(n.innerHTML) === 'prime') { yes.click(); } else { no.click(); } window.setTimeout(tick, 500); } start.click(); tick();
- mfoy_ 11y agoOr, do away with the whole "checking" nonsense: game.check = function (answer) { this.streak += 1; this.next_n(); } setInterval(function() { document.querySelector("#yes").click(); }, 0); document.querySelector("#start").click(); (High score of 15,000!)
- malka 11y agothis. IF you cheat, you might as well take the easiest path of all :D
- rMBP 11y ago"Inspect Element"
- gfmio 11y ago24607 (with the cheat), 621 (without) :D Fun evening challenge :P function isPrime(n){ if ((n<=1) || (!n % 2)) { return false; } for (var k = 3; k < Math.round(Math.sqrt(n) + 0.5); k = k+2) { if (n % k == 0) { return false; } } return true; } var nSpan = $("span#n") var yesButton = $("button#yes"); var noButton = $("button#no"); var body = $("body"); function solve(){ if (isPrime(parseInt(nSpan.innerHTML)) { yesButton.click(); } else { noButton.click(); } if (body.className != "end") { setTimeout(solve,0); } } solve();
- andrewstuart2 11y agoWhy only add 1? Why add anything at all? game.check = function() { this.streak = Number.MAX_VALUE; this.next_n(); }; document.querySelector("#start").click(); document.querySelector("#yes").click();
- XaspR8d 11y agoDecreasing the loop's upper bound to Math.sqrt(value) and only testing odd values after 2 are really simple ways to improve performance. Beyond that it'd be nice to build a list of previously determined primes to reduce your number of checks, but I guess at that point it might just be better to implement classic primality tests...
- xigency 11y agoIt's faster to check that the value isn't less than the loop counter squared than to perform a square root.
- parfe 11y agoNo it isn't. You only calculate the square root once, vs calculating the square at each iteration.
- JoeAltmaier 11y agoCalculating a square is ~1. A square root it ~N. Much harder to do. Its a common optimization in graphics code to compare squares for this reason.
- borkabrak 11y agoShe's right about squaring on each iteration, though. And granted, a square root is much more expensive, though done only once. Which method is faster would depend on the number of iterations.
- uxcn 11y agoThe latency for sqrtss on broadwell is 11 cycles with a throughput of 4, where mul is a latency of 3 and throughput of 1. So, using some concrete numbers, sqrt is more expensive, but not polynomially or even an order of magnitude.
- cyrusand 11y agoOr just: setInterval(function() {game.check(is_prime(game.current_n) === 'prime')}, 0);
- mjhoy 11y agowindow.is_prime = function () { return "prime"; }
- ajnas_kt 11y agoNow the game is, Who can write most efficient primality check algorithm!
- elwell 11y agoAt least write your own prime checking algorithm!
- andrewstuart2 11y agoHe initially did. Then decided to reuse the one already provided. The goal of cheating in this manner, after all, would be to match exactly the implementation the original programmer used. Even if it was buggy.
- nick_name 11y agoIf you don't want the browser to freeze ... var target = document.getElementById('n'); function detect() { n = parseInt(target.firstChild.nodeValue); if (typeof n === 'number' && is_prime(n) === 'prime') { console.log('>> YES,' + n + ' is prime, clicking "yes" ...'); document.getElementById('yes').click(); } else { console.log('>> NO,' + n + ' is not prime, clicking "no" ...'); document.getElementById('no').click(); } } var observer = new MutationObserver(function (mutations) { mutations.forEach(function (mutation) { window.setTimeout(detect, 500); }); }); observer.observe(target, { attributes: true, childList: true, characterData: true });
- deleted 11y ago[deleted]