
Is this prime? - jameshart
http://isthisprime.com/game/
======
tlrobinson
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?

~~~
XaspR8d
Decreasing 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
It's faster to check that the value isn't less than the loop counter squared
than to perform a square root.

~~~
Someone
If you go that way, it may be even faster (depending on you CPU, of course) to
use a recursion for computing the squares:

    
    
      (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)

~~~
uxcn
You would normally use something like newton-raphson to compute a square root,
which still isn't O(1) but converges fairly quickly. I would be very skeptical
that n _muls_ / _adds_ would be faster than an _sqrt_ , at very least for
x86-64 since it is implemented as an instruction.

~~~
Someone
My mental model of a CPU is a 6502 or, at best, a 68000 without FPU :-)

When asked about modern day performance, I would just say “I do not know”.
Thing is that those muls, on modern CPUs, might be pipelined in parallel with
the adds, so that, time wise, you would get them for free.

But of course, it might be possible to get that sqrt for free, too. Even if it
takes 50 cycles, you might start one, do a few iterations without checking for
‘reached sqrt(n) yet’, and then start a loop testing for the limit.

If you really want to know for x_64,
[http://www.agner.org/optimize/](http://www.agner.org/optimize/) probably has
the answers, but then, you would have to know the x86 instruction set, which
is horrendous (compared to that 6502 or 68000). Even realising that there will
be single, double and vector variants, the number of different instructions
with ‘SQRT’ in their name I find in
[http://www.agner.org/optimize/instruction_tables.pdf](http://www.agner.org/optimize/instruction_tables.pdf)
is insane.

And of course, probably, something completely out of left field could well be
the fastest way to do this (by a few ns, probably). For example, modern CPUs
can count leading zeros in an integer. If that instruction is fast (for x86,
that is not a given; ‘bit scan reverse’ was slow on some CPUs) subtract from
64/32/16, and halve, and you have a decent approximation to 2log(sqrt(n))
(using ‘if n has b bits, sqrt(n) has about b/2’)

~~~
waterhouse
Incidentally, even if you are using Newton's method to do square roots, it
helps significantly to start with a guess of the right size. If your initial
guess is way too large, each step will cut it down by about a factor of 1/2;
if it's way too small, it's equivalent (for finding sqrt(N)) to choosing
N/guess. Thus, if you're taking the square root of 2^64, and you start with 1,
your subsequent guesses will be roughly 2^63, 2^62, 2^61, 2^60, ..., taking a
total of 36 steps to reach the right integer; whereas if you started with
2^33, it takes (by my computation) only 5 steps to get to the right integer.

The effect is more significant with larger numbers:

    
    
      arc> (newton-steps (round (* 3/2 (expt 2 1000))) 1)
      508
      arc> (newton-steps (round (* 3/2 (expt 2 1000))) (expt 2 500))
      8

------
momavujisic
Am I the only one who thought this was an online tool to check if a product
was Prime-eligable on Amazon?

~~~
Diamons
I thought the same thing lol

------
timinman
Nice game. Something weird can happen in the brain when we have to choose a
binary answer under pressure. Maybe the fact that we are answering positively
for prime (which is not the default) adds to that. For me it helped to rename
'yes' to 'prime' and 'no' to 'divisible'.

~~~
cyborgx7
Same here. What happened to me is, instead of asking myself "Is this a prime
number?" I kept starting to ask myself "Can this number be expressed as a
multiplication of two other numbers?" for which the answers are obviously
reversed.

~~~
davidamarquis
Finding divisors is often hard when your number is large. It turns out the
condition that a number n can be expressed as a multiple of two other numbers
is equivalent to there being a number z less than n except for 1 and n-1 so
that (z^2 mod n) == 1.

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.

------
nosage
"1 is non-prime by definition."

dammit!

~~~
mediumdeviation
It's mainly due to the Fundamental Theorem of Arithmetic (aka unique prime
factorization) -
[https://en.wikipedia.org/wiki/Fundamental_theorem_of_arithme...](https://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic)

~~~
btilly
However this was not settled surprisingly late.

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.

~~~
OJFord
Still the case for the natural numbers (with 0 instead of 1). Though perhaps
more obvious why you would choose one or the other there.

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?)

~~~
nonbel
>(I wonder how many planets they teach there as being in our solar system?)

Your comment got me to look up the number of objects considered planets over
the years, this has ranged from 5-23. Data since 1543 is available here:
[https://dx.doi.org/10.1038%2Fscientificamerican0107-34](https://dx.doi.org/10.1038%2Fscientificamerican0107-34)

For fun I did a linear regression of year vs # planets and found that the
while the slope was 1.8 planets/century, this was not significantly different
from zero. The evidence appears to be consistent with a constant number of
planets.

------
valine
I always get tripped up on multiples of 13. I wonder why that is.

~~~
iopq
There is no easy way to tell multiples of 13. 91 is particularly hard because
it's a multiple of 7 and 13.

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.

~~~
madcaptenor
I've heard 91 called the "smallest non-obvious composite". Assume that testing
for divisiblity by 2, 3, 5, and 11 is easy - for 2 and 5 you just look at the
last digit, for 3 you take the sum of the digits, and with two-digit numbers a
number is divisible by 11 if and only if both its digits are the same. Also
assume that you can recognize squares. Then 91 = 7*13 is the first number that
you won't recognize as composite using these test.

~~~
sdenton4
The more general test for eleven is to check that the alternating sum of the
digits is zero.

~~~
cousin_it
Counterexample: 209. The even more general test is that the alternating sum of
digits is divisible by 11.

~~~
sdenton4
(I'll pretend to defend myself and say I meant zero mod eleven. :) )

------
raverbashing
Got 21

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)

~~~
waqf
_Au contraire_ , there are infinitely many bases in which "10" is prime.

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").

~~~
jaybosamiya
More specifically "10" is a prime in all prime bases.

------
rdl
I'd strongly prefer a UX of "Prime" "Composite" buttons, not "yes" "no". And
exclude 1; it's special (not-prime for good reasons, but also not composite;
Prime vs. Non-Prime isn't as good a test. Plus, treatment of one is largely
for definitional reasons and isn't intuitive to non-mathematicians.

Fun game, though!

~~~
vlasev
You'd have to exclude 1 if you were to do "prime" vs "composite" since 1 is
neither of those.

------
richardkiss
The following theorem, originally imparted to me by an algebra professor in
the UCLA math department (grad school), may be helpful to you when testing
yourself:

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.

~~~
sanderjd
That's awesome. Came here to say that 91 keeps killing my gut-instincts!

------
thomasahle
There is a nice rule I heard once: "Below 100 (119 really), any number that
looks like a prime is a prime. Except 49 and 91".

The remaining composite numbers are all trivially divisible by 2 or 5, or
their digit sum is divisible by 3.

~~~
transitorykris
Or the Grothendieck prime, 57

[https://en.wikipedia.org/wiki/57_(number)](https://en.wikipedia.org/wiki/57_\(number\))

~~~
cplease
Holy shit Grothendieck died in 2014, I NEVER MOURNED HIM. How did this happen?

------
mistercow
Interesting how after a few games, I just became terrible at this. I started
out getting 13-16 right before screwing up, but after a while, I was messing
up after two on numbers like 21 and 63. I think this game broke me.

------
ycmbntrthrwaway
Cheatsheet:
[https://en.wikipedia.org/wiki/Divisibility_rule](https://en.wikipedia.org/wiki/Divisibility_rule)

------
spuz
After a few goes my best score is 2. Considering I just wrote a program
yesterday to calculate primes this is pretty embarrassing.

------
gragas
I got 22 on my first try. The most helpful trick is to note that if the digits
add up to a multiple of 3 it cannot be prime (because it is divisible be 3)
with the exception of 3 itself.

------
Anderkent
Nice game. Reminds me of Infinite Certainty:
[http://www.spaceandgames.com/?p=27](http://www.spaceandgames.com/?p=27)

(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.

~~~
ilyanep
I got to 31 before I got tripped up on the exact same number. It _looks_
prime!

~~~
dylandrop
Trick: add up digits, if they are divisible by 3 so is the number. In this
case 117 yields 1+1+7 = 9, and sure enough so is 117. (117 / 3 == 39).

[https://en.wikipedia.org/wiki/Divisibility_rule#Divisibility...](https://en.wikipedia.org/wiki/Divisibility_rule#Divisibility_by_3_or_9)

~~~
ilyanep
Yeah, I had been doing that with other numbers, but somehow didn't trigger for
that one. Must have been the timing pressure.

------
photonios
Works on pretty much any browser:

    
    
      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")});

------
nevi-me
Any number whose digits sum up to a multiple of 3 (3,6,9) when summed until 1
digit remains; is a multiple of 3. Could this be something useful as a quick
check for qualifying numbers?

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.

------
ninju
You can check if a number is prime by going to the URL

[http://isthisprime.com/nnnnn..](http://isthisprime.com/nnnnn..).

where nnnn is a number (like
[http://isthisprime.com/5867868](http://isthisprime.com/5867868)) (note it
does take some time with BIG numbers)

------
p01926
There's a similar game on iOS — Infinite Primes.

[https://itunes.apple.com/us/app/infinite-
primes/id1063881848...](https://itunes.apple.com/us/app/infinite-
primes/id1063881848?mt=8)

------
binarymax
If we're playing Stargate Atlantis rules then these numbers should be 3, 4, or
5 digits long.

------
bradlenox
Simple but effective - I like that it provides an explanation when you get one
wrong.

------
Kenji
I've been asking myself lately: What is the first prime number after
googolplex?

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)

~~~
Chinjut
Well, it is what it is (the first prime number after a googolplex). What would
you prefer as a description of it? A listing of its digits? That would take
rather a long time to read out. I can start you off, though. Its first few
digits are 100000....

(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.

~~~
Kenji
Well, we could represent it by googolplex + x. I believe x cannot be 1 for it
to be prime due to number theoretical reasonings that I forgot in the
meantime.

 _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.

~~~
pvg
Imagine primality testing was O(p) where p is the number of bits in the number
you want to test. It is, in fact, way slower but let's give ourselves a huge
advantage. Testing any googolplex-sized number would still be impossible. This
seems to answer your question fairly simply and conclusively.

~~~
Kenji
I agree with you, there is no way we could compute it with our computers. But
there is still number theory with which you can analyze incomprehensibly large
numbers and make statements about them. Maybe you could say that the next 10
numbers after googolplex are not prime. That would already be a first step
towards an answer. You can't just categorically say it's impossible to find
out. It is just that our understanding of mathematics is far from being able
to answer this question. Anyway, it's more of a playful question I'm asking
just for fun. Whether or not we find an answer is not of importance to me.

~~~
pvg
It's a perfectly fun and good question, I just don't think the answer is that
hard. Short of a truly titanic mathematical breakthrough, it's not possible to
find out. Beside this being one of the oldest, most worked class of problems
in mathematics, the people doing these large and systematic searches are not
ignorant of number theory:

[https://en.wikipedia.org/wiki/Great_Internet_Mersenne_Prime_...](https://en.wikipedia.org/wiki/Great_Internet_Mersenne_Prime_Search)

[https://en.wikipedia.org/wiki/Repunit#Repunit_primes](https://en.wikipedia.org/wiki/Repunit#Repunit_primes)

While you can easily tell some numbers around googolplex are not prime
(googolplex + 1 and googolplex + 2 are obviously not) this doesn't tell you
anything useful about where the next prime might be. Testing a single such
gigantic number for primality is infeasible and there aren't exactly a lot of
prime numbers out there - on average, one every couple of googols as you can
see from:

[https://en.wikipedia.org/wiki/Prime_number_theorem](https://en.wikipedia.org/wiki/Prime_number_theorem)

Even if you could test them very quickly, you'd be looking for quite a while.

------
mbrumlow
I did mine the hard way. No java script here.

[https://www.youtube.com/watch?v=3ITOhyVemCU](https://www.youtube.com/watch?v=3ITOhyVemCU)

------
Houshalter
This is extremely relevant:
[http://www.spaceandgames.com/?p=27](http://www.spaceandgames.com/?p=27)

------
JosephRedfern
First go: I got 23 correct, one wrong. Fun game!

------
kazinator
Game needs a control panel where you can customize it.

I'd like to be able to declare that 51 is prime for the purposes of play.

------
danbolt
I'd love to see the statistics on which numbers get guessed right/wrong.

------
motdiem
Fun game - it would be great to have an achievement if your score is a prime
number too

------
eachro
Got 41. The ones over 100 took me quite a bit longer than the sub 100 numbers.

------
bigblind
My top score so far is 35.

------
autokad
i memorized all the prime numbers up to 100 for the GRE

------
billhendricksjr
Got 15 right. Also get caught up on multiples of 13

------
eva1984
29 right...yep

------
grimmdude
Nice game. 530 correct, first try

~~~
GavinMcG
I'm not sure which of us is sillier – you, for lying about this, or me, for
spending my time calling you out.

Or, hopefully, it was just a typo of 53. In which case I'd be impressed, since
I haven't topped 25.

~~~
grimmdude
Dang, HN is so harsh. Yea just a joke; I couldn't get past 5. Fun game though.

------
noobie
I guess programming ruined this sort of games for me.

~~~
boterock
I think programming converts the game into a different kind of game.

------
hellbanner
Save you all some time: [http://primecoin.io/](http://primecoin.io/)

