by tlrobinson · 10y ago · 0 comments
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?
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(); 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!) game.check = function() {
this.streak = Number.MAX_VALUE;
this.next_n();
};
document.querySelector("#start").click();
document.querySelector("#yes").click(); 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();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...
(n+1)^2 = n^2 + 2n + 1
That gets you: def squares:
n = 1
nSquared = 1
delta = 1
while nSquared <= n:
n += 1
yield n
delta += 2
nSquared += delta
but as others said, once your limit is large enough, the relatively large cost of computing a square root once may be lower than the O(sqrt(n)) additions in this loop.(Quick check: if you use a binary search to find sqrt(n), you need 2log(n) iterations, each of which requires a division by two (for integers, that is a bit shift) and one addition. A linear search takes sqrt(n) iterations, each with three additions. For whatever ratio of speed of operations you pick, the former will be faster for sufficiently large n)
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
}); setInterval(function() {game.check(is_prime(game.current_n) === 'prime')}, 0); window.is_prime = function () { return "prime"; }