icyrock.com

Home

PureScript solutions to Project Euler - problem 9

2018-Jun-23 23:11
purescriptproject-euler

Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module Euler009Test (euler9suite) where
 
import Prelude
 
import Euler009 (euler9)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
 
euler9suite :: forall e. TestSuite e
euler9suite =
  suite "Euler 9" do
    test "Warmup" do
      Assert.equal [60] (euler9 12)
    test "Real" do
      Assert.equal [31875000] (euler9 1000)

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module Euler009 (euler9) where
 
import Prelude
 
import Control.MonadZero (guard)
import Data.Array (range)
 
euler9 :: Int -> Array Int
euler9 n = do
  let am = n / 3
  a <- range 1 am
  let bm = (n - a) / 2
  b <- range (a + 1) bm
  let c = n - a - b
  guard $ a * a + b * b == c * c
  pure $ a * b * c