Created
August 24, 2015 19:01
-
-
Save jayrbolton/3c1c9d1bf82da5e0ea09 to your computer and use it in GitHub Desktop.
pe 3
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
{- | |
In Haskell | |
-} | |
primeFactors n = recur 2 [] n | |
where | |
recur divisor facs limit | |
| divisor > limit = facs | |
| limit `mod` divisor == 0 = recur divisor (divisor:facs) (limit `div` divisor) | |
| otherwise = recur (divisor+1) facs limit | |
// In Javascript | |
module.exports = primeFactors | |
function primeFactors(n) { | |
var limit = n, divisor = 2, facs = [] | |
while(divisor <= limit) { | |
if(limit % divisor === 0) { | |
facs.push(divisor) | |
limit = limit / divisor | |
} else { | |
++divisor | |
} | |
} | |
return facs | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment