Bläddra i källkod

[Haskell][12] Adding Mathematical solution

Vinicius Teshima 5 månader sedan
förälder
incheckning
9c360a8ef7
1 ändrade filer med 30 tillägg och 0 borttagningar
  1. 30 0
      haskell/src/0012.hs

+ 30 - 0
haskell/src/0012.hs

@@ -0,0 +1,30 @@
+{-
+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
+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?
+-}
+
+import Data.List
+import Data.Maybe
+
+n_divs :: Int -> Int
+n_divs n = length (filter (\x -> (mod n x) == 0) [1..n])
+
+-- Even Number Always have more divisors than Odd
+solution :: Int
+solution = fromJust (find (\x -> (n_divs x) > 500) (iterate (+2) 2))
+
+{-
+This program has never run to conclusion, but it is mathematically correct so it stays
+-}
+main :: IO ()
+main = putStrLn ("Solution: " ++ show solution)