1
0
Fork 0

Performance improvements with math

main
Bill Ewanick 2023-09-15 02:47:36 -04:00
parent 38ba59c3e4
commit 8f5004d6ff
1 changed files with 32 additions and 0 deletions

View File

@ -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
-}