mirror of
https://github.com/correl/euler.git
synced 2024-11-23 19:19:53 +00:00
Fixed Haskell 003
This commit is contained in:
parent
09031a099d
commit
2c0a857bd4
1 changed files with 11 additions and 12 deletions
|
@ -8,20 +8,19 @@ What is the largest prime factor of the number 600851475143 ?
|
||||||
import Text.Printf
|
import Text.Printf
|
||||||
|
|
||||||
pfactor :: (Integral a) => a -> [a]
|
pfactor :: (Integral a) => a -> [a]
|
||||||
pfactor n = pfactor_ n []
|
pfactor n = pfactor_ n [] 2
|
||||||
|
|
||||||
pfactor_ :: (Integral a) => a -> [a] -> [a]
|
pfactor_ n factors start =
|
||||||
pfactor_ n factors = do
|
let next = pfactor_next n start in
|
||||||
let next = pfactor_next n 2
|
if n == next
|
||||||
if next == n
|
then n:factors
|
||||||
then factors ++ [n]
|
else pfactor_ (n `div` next) (next:factors) next
|
||||||
else pfactor_ (n `div` next) (factors ++ [next])
|
|
||||||
|
|
||||||
pfactor_next :: (Integral a) => a -> a -> a
|
pfactor_next :: (Integral a) => a -> a -> a
|
||||||
pfactor_next n factor
|
pfactor_next n last
|
||||||
| n == factor = factor
|
| n <= last = n
|
||||||
| n `rem` factor == 0 = factor
|
| otherwise = head (filter (\x -> n `rem` x == 0 || n == x) [last..n])
|
||||||
| otherwise = pfactor_next n (factor + 1)
|
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
printf "Largest prime factor of 600851475143: %d\n" (last (pfactor 600851475143) :: Int)
|
printf "Largest prime factor of 13195: %d\n" (head (pfactor 13195) :: Integer)
|
||||||
|
printf "Largest prime factor of 600851475143: %d\n" (head (pfactor 600851475143) :: Integer)
|
||||||
|
|
Loading…
Reference in a new issue