Created
March 11, 2020 17:13
-
-
Save arifd/f4262c2e062d3faf010335500a89577b to your computer and use it in GitHub Desktop.
Prime numbers & factors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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