216 points · by jameshart · 10y ago · 123 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"; }This condition is the basis of the Miller-Rabin primality test. Sadly its a bit hard for humans to implement this algorithm for mentally proving primality.
Also, Prime itself is a tricky term for a Yes-No question as it's really a negative concept - the absence of something. So asking if X is prime is requiring double-negative logic in the same way as an app setting like "Disable X feature [On / Off]".
dammit!
I have seen tables of prime counts produced in the 1950s and 1960s that specified whether they counted 1 as a prime, and some did count it.
"The way mathematicians have viewed the number one (unity, the monad) has changed throughout the years. Most of the early Greeks did not view one as a number, but rather as the origin, or generator, of number. Around the time of Simon Stevin (1548-1620), one (and zero) were first widely viewed as numbers. This created a period of confusion about whether or not the number one was prime. In this dynamic survey, we collect a cornucopia of sources which deal directly with the "question what is the smallest prime?" The goal is to create a source book for studying the history of the definition of prime, especially as applied to the number one."
Going through school I'd be told 1 was/not prime differently by different teachers. Always seemed pretty arbitrary to me; like 'They' ought just to decide!
What we seem to be hearing here is that They had, just another classic case of this information taking 50 years to filter down the education system.. (I wonder how many planets they teach there as being in our solar system?)
Obviously 5 * 13 is too easy. There's a test for division by 3, so 9 * 13 is too easy.
So basically you should only miss 91.
Funny fact, the number 100, in any base (>2 obviously) is not prime (updated)
In base 10, the number can only be prime if it ends in 1, 3, 7, 9 (where 10-1 = 9 and 10 - 3 = 7) - condition necessary but not sufficient of course
If all digits sum to a multiple of 3 it is divisible by 3, of course (because 10^X mod 3 is 1 for any integer X)
For 7 you want 3x the 2nd digit (21 - 2x3 + 1 = 7, divisible by 7) and 2x the 3rd digit (so 119 - 2x1 + 3x1 + 9 = 14)
However, "100" is an example of a digit string which is not prime in any base. Another is "1001" (it's always divisible by "11").
Fun game, though!
Theorem. All numbers < 100 that look prime, are prime EXCEPT 91.
(My addendum)
Where "look prime" means, is not a multiple of 2, 3, 5, 11, nor a perfect square. All these numbers have quick tests for divisibility.
Note that 91 = 7 * 13.
The remaining composite numbers are all trivially divisible by 2 or 5, or their digit sum is divisible by 3.
(now, granted there's a catch: question like 'is 89 prime' is harder than 'is 5 prime'; so it makes sense to have very high confidence of the latter (99.99% sounds reasonable) while much lower of the former)
p.s. Game over
117 = 32×13
You correctly sorted 20 numbers. Feel I did pretty well.
https://en.wikipedia.org/wiki/Divisibility_rule#Divisibility...
var element=document.getElementById("n");element.addEventListener("DOMSubtreeModified",function(){function a(a){setTimeout(function(){var b=document.createEvent("Events");b.initEvent("click",!0,!1),document.getElementById(a).dispatchEvent(b)},1)}var b=parseInt(element.innerText);a("prime"===is_prime(b)?"yes":"no")});I don't have any Assembly knowledge, but I presume that it wouldn't be efficient implementing this, because of having to cast to string, then back from string for each digit. Then the while loop to sum each digit.
Running the check only for odd numbers could also be an improvement.
http://isthisprime.com/nnnnn...
where nnnn is a number (like http://isthisprime.com/5867868) (note it does take some time with BIG numbers)
https://itunes.apple.com/us/app/infinite-primes/id1063881848...
If you have an advice or know the answer, please do tell :)
(This question appears to be very hard and it seems like nobody has the answer yet)
http://www.mersenne.org/primes/?press=M74207281
It seems testing and searching googolplex-sized numbers would be far beyond wildly impractical.
(It's easy enough to write a program that will eventually, slowly, find and stream to the display its digits or any other basic information about it you are interested in, of course. It's just infeasibly slow to use such a thing at the moment.)
The current largest "known" prime, for some sense of what it is to indicate a specific number and know it to be prime, has about 20 million digits (and a very specific convenient form). The prime you are looking for will have about a googol digits instead. So, we're far off, at the moment, from being able to figure out the sort of information about it you're presumably interested in.
So, we're far off, at the moment, from being able to figure out the sort of information about it you're presumably interested in.
I'm not "presumably interested". I'm genuinely interested and curious about it. I'm just hesitant to invest a lot of time in it because of the unlikelihood of success.
Mostly, it's just a hedging phrase that I felt the need to write which carries no great meaning. (And, in some sense, this whole post is similar...)
https://en.wikipedia.org/wiki/Cunningham_project#Other_facto...
I'd like to be able to declare that 51 is prime for the purposes of play.
Or, hopefully, it was just a typo of 53. In which case I'd be impressed, since I haven't topped 25.