Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Stack overflow when constructing a vector, an easier solution.
-- See http://stackoverflow.com/questions/15991634/why-i-take-a-message-for-stack-overflow-in-haskell/15992529#15992529
import Control.DeepSeq (($!!))
import Data.Vector as V hiding ((++))
import System.Environment
d :: Vector Integer
d = generate 1000000
(\z -> case z of
0 -> 2
1 -> 3
2 -> 5
_ -> if odd z then (d ! (z-1)) +2 else (d ! (z-1)) + 4)
algorithmA :: Integer -> Int -> Integer -> [Integer] -> [Integer]
algorithmA _ _ 1 pt = pt
algorithmA t k n pt =
let dk = d ! k
q = div n dk
r = mod n dk
in if r /=0 then
if q>dk then
algorithmA t (k+1) n pt
else (n:pt)
else
algorithmA (t+1) k q (dk:pt)
main :: IO ()
main = do
args <- getArgs
let n = read (args !! 0)
let l = V.last $!! d
if (floor(sqrt(fromIntegral n))) > l
then error ("The square root of number is greater than "
++ show l)
else
print (algorithmA 0 0 n [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.