Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- The sequence of triangle numbers is generated by adding the natural
- -- numbers. So the 7th triangle number would be
- -- 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3,
- -- 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the
- -- first seven triangle numbers: 1: 1; 3: 1, 3; 6: 1, 2, 3, 6; 10: 1,
- -- 2, 5, 10; 15: 1, 3, 5, 15; 21: 1, 3, 7, 21; 28: 1, 2, 4, 7, 14, 28
- -- We can see that 28 is the first triangle number to have over five
- -- divisors. What is the value of the first triangle number to have
- -- over five hundred divisors?
- -- Generate the infinite list of triangle numbers.
- tn :: [Integer]
- tn = scanl1 (+) [1 ..]
- -- Take a number and return a tuple with the number and the number of
- -- its divisors.
- nad :: Integral c => c -> (c, Int)
- nad n = (n, d * 2 - sna)
- where
- d
- | mod n 2 == 0 = length [q | q <- [1 .. srf n], mod n q == 0]
- | otherwise = length [q | q <- [1,3 .. srf n], mod n q == 0]
- srf = floor . sqrt . fromIntegral
- src = ceiling . sqrt . fromIntegral
- sna
- | srf n /= src n = 0
- | otherwise = 1
- -- Check if the number of divisors is greater than five hundred.
- ndgfh :: (Ord a, Num a) => (t, a) -> Bool
- ndgfh (_, d)
- | d > 500 = True
- | otherwise = False
- main :: IO ()
- main = print $ fst $ head $ filter ndgfh $ map nad tn
- -- 76576500
- -- real 0m8,009s
- -- user 0m7,966s
- -- sys 0m0,032s
Add Comment
Please, Sign In to add comment