Ad

Optimization: you don't need to go all the way to n.

Code
Diff
  • export const checkIsPrimeNumber = (n: number) => 
    {
      if (!Number.isInteger(n)) return false;
      for (var i = 2; i <= Math.min(n, Math.sqrt(n)); i++) {
        if (n % i === 0) return false;
      }
      return n > 1;
    };
    • export const checkIsPrimeNumber = (n: number) =>
    • {
    • if (!Number.isInteger(n)) return false;
    • for (var i = 2; i < n; i++) {
    • for (var i = 2; i <= Math.min(n, Math.sqrt(n)); i++) {
    • if (n % i === 0) return false;
    • }
    • return n > 1;
    • };

Bitwise operations usually work faster than arithmetic.

Code
Diff
  • power_of_two = lambda n: not n&1
    • power_of_two = lambda n: n == (n>>1)<<1
    • power_of_two = lambda n: not n&1

Might work slightly faster.

Code
Diff
  • power_of_two = lambda n: n == (n>>1)<<1
    • power_of_two = lambda n: not n%2
    • power_of_two = lambda n: n == (n>>1)<<1