Permalink
Browse files

Initial Cabal project with problem 1 code and test.

  • Loading branch information...
0 parents commit 9f47f0022529bcbdd908e361b967df96d50a5f6b @FranklinChen committed Nov 5, 2012
Showing with 145 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +18 −0 Answer/answer1.hs
  3. +21 −0 Euler/Problem1.hs
  4. +30 −0 LICENSE
  5. +10 −0 README.md
  6. +2 −0 Setup.hs
  7. +21 −0 Test/Test1.hs
  8. +41 −0 project-euler-haskell.cabal
@@ -0,0 +1,2 @@
+.DS_Store
+dist
@@ -0,0 +1,18 @@
+{-
+ Problem 1
+
+ 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.
+-}
+
+module Main where
+
+import Euler.Problem1
+
+main :: IO ()
+main = putStrLn $ show answer
+
+answer :: Int
+answer = sumMultiple3or5Below 1000
@@ -0,0 +1,21 @@
+{-
+ Problem 1
+
+ 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.
+-}
+
+module Euler.Problem1 where
+
+sumMultiple3or5Below :: Int -> Int
+sumMultiple3or5Below n = sum $ filter multiple3or5 [1..n-1]
+
+{-
+The trick is to avoid double-counting numbers that are multiples of
+both 3 and 5. So we cannot count multiples of 3 and multiples of 5
+and add them.
+-}
+multiple3or5 :: Int -> Bool
+multiple3or5 n = n `mod` 3 == 0 || n `mod` 5 == 0
30 LICENSE
@@ -0,0 +1,30 @@
+Copyright (c) 2012, Franklin Chen
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Franklin Chen nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,10 @@
+## [Haskell](http://www.haskell.org/) solutions to [Project Euler](http://projecteuler.net/) problems
+
+This repository uses [Cabal](http://www.haskell.org/cabal/) for building and testing.
+
+To use it:
+
+- Install a recent version of [Glasgow Haskell Compiler](http://www.haskell.org/ghc/).
+- Install a recent version of Cabal.
+- `$ cabal build && cabal test`
+- Run the sample executable: `$ dist/build/answer1/answer1`
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
@@ -0,0 +1,21 @@
+{-# LANGUAGE TemplateHaskell #-}
+
+module Main where
+
+import Test.Framework.TH (defaultMainGenerator)
+import Test.Framework.Providers.HUnit (testCase)
+import Test.HUnit
+
+import Euler.Problem1 (multiple3or5, sumMultiple3or5Below)
+
+main = $(defaultMainGenerator)
+
+case_1 = do
+ multiple3or5 30 @?= True
+
+case_2 = do
+ multiple3or5 4 @?= False
+
+case_3 = do
+ sumMultiple3or5Below 10 @?= 23
+
@@ -0,0 +1,41 @@
+name: project-euler-haskell
+version: 0.1.0.0
+synopsis: Solutions to Project Euler problems
+description: This package contains solutions to Project Euler problems.
+homepage: http://github.com/FranklinChen/project-euler-haskell
+bug-reports: http://github.com/FranklinChen/project-euler-haskell/issues
+license: BSD3
+license-file: LICENSE
+copyright: (c) 2012 Franklin Chen
+author: Franklin Chen <franklinchen@franklinchen.com>
+maintainer: franklinchen@franklinchen.com
+category: Math
+build-type: Simple
+cabal-version: >=1.8
+extra-source-files: .gitignore,
+ README.md
+
+source-repository head
+ type: git
+ location: http://github.com/FranklinChen/project-euler-haskell.git
+
+library
+ build-depends: base
+ exposed-modules: Euler.Problem1
+
+executable answer1
+ main-is: Answer/answer1.hs
+ build-depends: base,
+ project-euler-haskell
+
+-- detailed-0.9 problem integrating with HUnit?
+test-suite test-problem1
+ type: exitcode-stdio-1.0
+ main-is: Test/Test1.hs
+ build-depends: base,
+ project-euler-haskell,
+ test-framework,
+ test-framework-hunit,
+ test-framework-th,
+ HUnit
+

0 comments on commit 9f47f00

Please sign in to comment.