Selaa lähdekoodia

[Haskell][6] Adding solution

Vinicius Teshima 5 kuukautta sitten
vanhempi
sitoutus
7e6f5c2228
1 muutettua tiedostoa jossa 20 lisäystä ja 0 poistoa
  1. 20 0
      haskell/src/0006.hs

+ 20 - 0
haskell/src/0006.hs

@@ -0,0 +1,20 @@
+{-
+The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + ... + 10^2 = 385.
+The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)^2 = 55^2 = 3025.
+Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640.
+Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
+-}
+
+solution :: Int
+solution = (square_of_sum 1 0) - (sum_of_square 1 0)
+  where
+    limit = 100
+    sum_of_square x res
+      | x > 100   = res
+      | otherwise = sum_of_square (x + 1) (res + (x * x))
+    square_of_sum x res
+      | x > 100   = res*res
+      | otherwise = square_of_sum (x + 1) (res + x)
+
+main :: IO ()
+main = putStrLn ("Solution: " ++ show solution)