Skip to content

Instantly share code, notes, and snippets.

@WillNess
Last active December 17, 2015 00:59
Show Gist options
  • Save WillNess/5525556 to your computer and use it in GitHub Desktop.
Save WillNess/5525556 to your computer and use it in GitHub Desktop.
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