Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Giuga-numbers/Haskell/giuga-numbers.hs
Normal file
20
Task/Giuga-numbers/Haskell/giuga-numbers.hs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
--for obvious theoretical reasons the smallest divisor of a number bare 1
|
||||
--must be prime
|
||||
primeFactors :: Int -> [Int]
|
||||
primeFactors n = snd $ until ( (== 1) . fst ) step (n , [] )
|
||||
where
|
||||
step :: (Int , [Int] ) -> (Int , [Int] )
|
||||
step (n , li) = ( div n h , li ++ [h] )
|
||||
where
|
||||
h :: Int
|
||||
h = head $ tail $ divisors n --leave out 1
|
||||
|
||||
divisors :: Int -> [Int]
|
||||
divisors n = [d | d <- [1 .. n] , mod n d == 0]
|
||||
|
||||
isGiuga :: Int -> Bool
|
||||
isGiuga n = (divisors n /= [1,n]) && all (\i -> mod ( div n i - 1 ) i == 0 )
|
||||
(primeFactors n)
|
||||
|
||||
solution :: [Int]
|
||||
solution = take 4 $ filter isGiuga [2..]
|
||||
Loading…
Add table
Add a link
Reference in a new issue