Optimization: you don't need to go all the way to n.
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.
Might work slightly faster.