Skip to content

Instantly share code, notes, and snippets.

@NicolasFrancaX
Created September 26, 2016 11:58
Show Gist options
  • Save NicolasFrancaX/5a7c33f39eeb4dfaa3282ab3b3e6f1ae to your computer and use it in GitHub Desktop.
Save NicolasFrancaX/5a7c33f39eeb4dfaa3282ab3b3e6f1ae to your computer and use it in GitHub Desktop.
/* (**) Determine the prime factors of a given positive integer.
* Construct a flat list containing the prime factors in ascending order.
* Example:
* * (prime-factors 315)
* (3 3 5 7)
*/
function prime(n) {
if (n < 2)
return false;
var i = 2;
while(i < n) {
if (n % i == 0)
return false;
i++;
}
return true
}
function primeFactors(n) {
var numbers = [];
for(var i = 2; i < n; i++)
if (n % i == 0 && prime(i))
numbers.push(i);
return numbers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment