Created
September 26, 2016 11:57
-
-
Save NicolasFrancaX/0cee3c33c1635b648a2fdf00c3f7d65e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* (**) 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; | |
} else if (n == 2) { | |
return true; | |
} else if (n % 2 == 0) { | |
return false; | |
} | |
var i = 3; | |
while(i < n) { | |
if (n % i == 0) | |
return false; | |
i += 2; | |
} | |
return true | |
} | |
function primeFactors(n) { | |
var numbers = []; | |
if (n < 2) | |
return numbers; | |
numbers.push(2); | |
for(var i = 3; i < n; i += 2) { | |
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