Skip to content

Instantly share code, notes, and snippets.

@arifd
Created March 11, 2020 17:13
Show Gist options
  • Save arifd/f4262c2e062d3faf010335500a89577b to your computer and use it in GitHub Desktop.
Save arifd/f4262c2e062d3faf010335500a89577b to your computer and use it in GitHub Desktop.
Prime numbers & factors
export function listFactors(target) {
let primes = listPrimes(target);
let factors = [];
for (let i = 0; i < primes.length; i++) {
if ((target % primes[i]) === 0) factors.push(primes[i]);
}
return factors;
}
// list all prime numbers up to n
export function listPrimes(n) {
let primes = [];
for (let i = 2; i < n; i++) {
if (isPrime(i)) primes.push(i);
}
return primes;
}
function isPrime(n) {
for (let i = 2; i < n; i++){
if ((n % i) === 0) return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment