Project euler problem 3 in haskell

Posted by shk on Stack Overflow See other posts from Stack Overflow or by shk
Published on 2011-07-01T06:49:41Z Indexed on 2011/07/01 8:22 UTC
Read the original article Hit count: 230

Filed under:
|
|

I'm new in Haskell and try to solve 3 problem from http://projecteuler.net/.

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

My solution:

import Data.List

getD :: Int -> Int
getD x = 
  -- find deviders
  let deriveList = filter (\y -> (x `mod` y) == 0) [1 .. x]
      filteredList = filter isSimpleNumber deriveList
  in maximum filteredList

-- Check is nmber simple
isSimpleNumber :: Int -> Bool
isSimpleNumber x = let deriveList = map (\y -> (x `mod` y)) [1 .. x]
                       filterLength = length ( filter (\z -> z == 0) deriveList)
                       in 
                          case filterLength of
                            2 -> True
                            _ -> False

I try to run for example:

getD 13195
> 29

But when i try:

getD 600851475143

I get error Exception: Prelude.maximum: empty list Why?

Thank you @Barry Brown, I think i must use:

getD :: Integer -> Integer

But i get error:

Couldn't match expected type `Int' with actual type `Integer'
Expected type: [Int]
  Actual type: [Integer]
In the second argument of `filter', namely `deriveList'
In the expression: filter isSimpleNumber deriveList

Thank you.

© Stack Overflow or respective owner

Related posts about haskell

Related posts about project