Prechádzať zdrojové kódy

[Haskell][1] Adding Solution

Vinicius Teshima 5 mesiacov pred
rodič
commit
8831a8b4ae
3 zmenil súbory, kde vykonal 39 pridanie a 0 odobranie
  1. 5 0
      haskell/Makefile
  2. 9 0
      haskell/build.sh
  3. 25 0
      haskell/src/0001.hs

+ 5 - 0
haskell/Makefile

@@ -0,0 +1,5 @@
+
+
+all:
+	@echo 'Running build.sh'
+	@./build.sh

+ 9 - 0
haskell/build.sh

@@ -0,0 +1,9 @@
+#!/bin/sh
+
+find ./src -name '*.hs' -printf '%T@ %p\n' \
+	| sort -r | head -n1 | cut -d' ' -f2 |  while read file
+do
+	out_file="$(basename "$file" | cut -d'.' -f1)"
+	echo "Compiling file '${file}' into '${out_file}'"
+	ghc -o "$out_file" "$file"
+done

+ 25 - 0
haskell/src/0001.hs

@@ -0,0 +1,25 @@
+
+{-
+If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
+
+Find the sum of all the multiples of 3 or 5 below 1000.
+-}
+
+solution_is_div :: Int -> Int
+solution_is_div x = case (mod x 3) of
+  0 -> x
+  _ -> case (mod x 5) of
+        0 -> x
+        _ -> 0
+
+solution_inner :: Int -> Int
+solution_inner x = go' x 0 where
+  go' 1000 y = y
+  go' x y = go' (x+1) ((solution_is_div x) + y)
+
+solution :: Int
+solution =
+  solution_inner 0
+
+main :: IO ()
+main = putStrLn ("Solution: " ++ show solution)