-1

I missed this optimization in an interview and wanted to review the algebra that allows it:

for(let j = 2; j * j < i; j++){

The line above in the code below, were instead of using j < i, we use j * j < i or equivalently, j < \sqrt{i}.

Is this basic algebra we learned in grade school that I forgot? Can a reference be provided to an algebra cheat sheet or similar?

// prime-2
// 2 optimizations - odds and square root
function prime2(n){
  const primes = [2];

  // first optimization is to only check odd numbers ...
  not_prime: for(let i = 3; i < n; i += 2){

    // second optimization is to only check up to ...
    // the square root of j when looking for factors
    for(let j = 2; j * j < i; j++){
      if(i % j === 0){
        continue not_prime;
      }
    }
    primes.push(i);
  }
  return primes;
}
jennifer
  • 682
  • 5
  • 14
  • 2
    Does this answer your question? [Why do we check up to the square root of a prime number to determine if it is prime?](https://stackoverflow.com/questions/5811151/why-do-we-check-up-to-the-square-root-of-a-prime-number-to-determine-if-it-is-pr) – benbotto Feb 25 '20 at 16:16
  • I'm 100% sure there is an algebraic identity or similar for this ... I just can't recall it. – jennifer Feb 25 '20 at 16:23
  • Then it sounds like this should be on [math.se] if you're specifically asking about algebra. –  Feb 25 '20 at 16:34
  • 2
    If n = a\*b and a <= b then a\*a <= a\*b = n – benbotto Feb 25 '20 at 16:39
  • @j.a. It's from the link in the top comment. –  Feb 25 '20 at 16:43
  • 1
    No, this question is a duplicate and should be closed as such. Further, the statement's truth is obvious and easily proven: pick any a > sqrt(n), and b >= a, then a*b > n, obviously. – benbotto Feb 25 '20 at 16:44
  • ... express it in one way ... the idea is obvious. Relating it to the outside world of Algebra and giving it a name is not so easy. There is this thing called Algebra that CS uses to solve problems. I know how it works. I want the law or identity in a single referenced form. – jennifer Feb 25 '20 at 17:02
  • See here for examples ... https://www.eeweb.com/tools/algebra-reference-sheet – jennifer Feb 25 '20 at 17:02
  • Should be under Algebra - radical properties ... – jennifer Feb 25 '20 at 17:04
  • @avejidah I agree this should be closed, but for a different reason. It's actually a math question, not a programming one. –  Feb 25 '20 at 17:15
  • @Amy Yeah, I agree with that; however, the question has been answered numerous times on math.stackexchange.com, too, directly and indirectly. https://math.stackexchange.com/questions/35072/calculating-prime-numbers https://math.stackexchange.com/questions/1343171/why-only-square-root-approach-to-check-number-is-prime/1343671#1343671 – benbotto Feb 25 '20 at 17:24
  • @avejidah Yes, and it's been answered several times here, too, both in these comments and below. –  Feb 25 '20 at 17:27

2 Answers2

2

LeetCode has this as a problem (Count Primes) and has a helpful hint to help you understand this:

Let's write down all of 12's factors:

2 × 6 = 12

3 × 4 = 12

4 × 3 = 12

6 × 2 = 12

As you can see, calculations of 4 × 3 and 6 × 2 are not necessary. Therefore, we only need to consider factors up to √n because, if n is divisible by some number p, then n = p × q and since p ≤ q, we could derive that p ≤ √n.

Daniel W Strimpel
  • 8,190
  • 2
  • 28
  • 38
  • I swear someone provided an algebraic identity for this. Not some intuitively derived equation that makes sense. There is an algebraic law for this. – jennifer Feb 25 '20 at 16:36
  • You could read through the Wikipedia article on prime numbers (https://en.wikipedia.org/wiki/Prime_number) to understand more about them and when this idea came up of not needing to go above the √n (found in the beginning of the History section). You can also read the "Trial division" section there (or the separate wiki page - https://en.wikipedia.org/wiki/Trial_division) to understand more about this method. – Daniel W Strimpel Feb 25 '20 at 16:50
0

I think it should be j * j <= i. The reasoning is that if i is not a prime then you have i == p*q where p is a prime smaller-or-equal q i.e. p<=q. So for example in the worst case 49 is 7*7. You don't need to check beyond p e.g. 7+ because you already checked for the smaller prime.

Adder
  • 5,708
  • 1
  • 28
  • 56
  • I'm 100% sure there is an algebraic identity or similar for this ... I just can't recall it. – jennifer Feb 25 '20 at 16:23
  • What do you mean by worst case ... there is only 1 case. The algorithm has only input n. Your answer makes sense intuitively, but I'm asking for the law that proves it. – jennifer Feb 25 '20 at 16:34