How to create the most compact mapping n → isprime(n) up to a limit N?

后端 未结 30 3219
遇见更好的自我
遇见更好的自我 2020-11-22 02:11

Naturally, for bool isprime(number) there would be a data structure I could query.
I define the best algorithm, to be the algorithm that pr

30条回答
  •  孤城傲影
    2020-11-22 02:39

    best algorithm for Primes number javascript

     function isPrime(num) {
          if (num <= 1) return false;
          else if (num <= 3) return true;
          else if (num % 2 == 0 || num % 3 == 0) return false;
          var i = 5;
          while (i * i <= num) {
            if (num % i == 0 || num % (i + 2) == 0) return false;
            i += 6;
          }
          return true
        }
    

提交回复
热议问题