Last active
December 17, 2015 00:59
-
-
Save WillNess/5525556 to your computer and use it in GitHub Desktop.
improved divStep
http://stackoverflow.com/questions/16387032/how-to-write-this-as-a-single-function/16399024#16399024
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
I'm late to the party but here goes: | |
> divStep (start, n) = head $ [(x,q) | x <- [start .. isqrt n], | |
> let (q,r)=quotRem n x, r == 0] ++ [(n,1)] | |
> isqrt n = floor . sqrt . fromIntegral $ n | |
> pe3 n | n > 1 = fst . until ((== 1) . snd) divStep $ (2,n) | |
> factorizing n = takeWhile ((> 1) . fst) . drop 1 . iterate divStep $ (2,n) | |
> factors n = map fst . factorizing $ n | |
> isPrime n = factors n == [n] | |
as developed and explained here - http://stackoverflow.com/questions/15699820/ | |
racket-programming-where-am-i-going-wrong/15703327#15703327 . | |
It produces the factors of a number, that by construction are guaranteed to be prime, | |
so no additional testing is necessary. | |
You rightfully felt weird in this particular case, because your `get_factors` is | |
nothing but a `filter`, as others pointed out. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment