Performance improvements with math
parent
38ba59c3e4
commit
8f5004d6ff
|
@ -0,0 +1,32 @@
|
||||||
|
-- module Main where
|
||||||
|
import Data.IntSet (size)
|
||||||
|
import Data.List (unfoldr)
|
||||||
|
import Math.NumberTheory.ArithmeticFunctions (divisorsSmall)
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = print ans
|
||||||
|
|
||||||
|
-- triangleNumber :: Integer -> [Integer]
|
||||||
|
-- triangleNumber 1 = [1]
|
||||||
|
-- triangleNumber n = sum [1..n] : triangleNumber (n-1)
|
||||||
|
triangleNumber i = (i * (i + 1)) `div` 2
|
||||||
|
triangleNumbers = map triangleNumber [1..]
|
||||||
|
-- triangleNumbers :: [Int]
|
||||||
|
-- triangleNumbers = unfoldr (\b-> Just (sum [1..b],b+1)) 1
|
||||||
|
|
||||||
|
-- isDivisible a b = a `rem` b == 0
|
||||||
|
-- divisors n = filter (isDivisible n) [1..(floor . sqrt) n ]
|
||||||
|
|
||||||
|
-- Triangle Numbers With More than N divisors
|
||||||
|
tnwmtNd :: Int -> [Int]
|
||||||
|
tnwmtNd n = filter ((>= n) . size . divisorsSmall) triangleNumbers
|
||||||
|
|
||||||
|
ans :: Int
|
||||||
|
ans = head $ tnwmtNd 500
|
||||||
|
{-
|
||||||
|
λ> main
|
||||||
|
76576500
|
||||||
|
(0.00 secs, 114,944 bytes)
|
||||||
|
|
||||||
|
First triangle number to have over 500 divisors is 76576500
|
||||||
|
-}
|
Loading…
Reference in New Issue