icyrock.com

Home

PureScript solution to Project Euler problem 28

2020-Jan-26 12:12
purescriptproject-euler

Problem details at Project Euler problem 28 page.

Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module Euler028Test (euler28suite) where
 
import Prelude
 
import Euler028 (euler28)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
 
euler28suite :: TestSuite
euler28suite =
  suite "Euler 28" do
    test "Warmup" do
      Assert.equal 101 (euler28 5)
    test "Real" do
      Assert.equal 669171001 (euler28 1001)

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module Euler028 where
 
import Prelude
 
import Data.Array (range)
import Data.Foldable (sum)
 
corners :: Int -> Array Int
corners n
  | n == 1    = [1]
  | otherwise =
      let j = 1 + (n - 1) * 2
          k = j * j
          f l = k - l * (j - 1)
      in map f (range 0 3)
 
euler28 :: Int -> Int
euler28 n = sum $ join $ map corners $ range 1 (n / 2 + 1)