Skip to content

Instantly share code, notes, and snippets.

@floydpink
Created February 1, 2022 16:07
Show Gist options
  • Save floydpink/d2edeba5142e2181d2985b060f9c0952 to your computer and use it in GitHub Desktop.
Save floydpink/d2edeba5142e2181d2985b060f9c0952 to your computer and use it in GitHub Desktop.
Find Prime Numbers
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const isPrime = (num: number) => {
const sqRoot = Math.floor(Math.sqrt(num));
let prime = num != 1;
for (let i = 2; i < sqRoot + 1; i++) {
if (num % i == 0) {
prime = false;
break;
}
}
return prime;
};
const result = numbers.filter((cr) => isPrime(cr));
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment