Add separate ProjectEuler.hs module

This commit is contained in:
daniele 2024-03-29 20:18:11 +01:00
parent aad8467fb9
commit 85841be19b
Signed by: fuxino
GPG Key ID: 981A2B2A3BBF5514
2 changed files with 13 additions and 9 deletions

12
Haskell/ProjectEuler.hs Normal file
View File

@ -0,0 +1,12 @@
module ProjectEuler
( isPrime
) where
isPrime :: (Integral n) => n -> Bool
isPrime 1 = False
isPrime 2 = True
isPrime 3 = True
isPrime n =
n `mod` 2 /= 0 && n `mod` 3 /= 0 && null [ x | x <- candidates, n `mod` x == 0 || n `mod` (x+2) == 0 ]
where candidates = [5,11..limit]
limit = floor(sqrt(fromIntegral n)) + 1

View File

@ -1,14 +1,6 @@
-- The prime factors of 13195 are 5, 7, 13 and 29. --
-- What is the largest prime factor of the number 600851475143?
isPrime :: (Integral n) => n -> Bool
isPrime 1 = False
isPrime 2 = True
isPrime 3 = True
isPrime n =
n `mod` 2 /= 0 && n `mod` 3 /= 0 && null [ x | x <- candidates, n `mod` x == 0 || n `mod` (x+2) == 0 ]
where candidates = [5,11..limit]
limit = floor(sqrt(fromIntegral n)) + 1
import ProjectEuler
maxPrimeFactor :: (Integral n) => n -> n
maxPrimeFactor n