icyrock.com

Home

PureScript solutions to Project Euler - problem 6

2018-Mar-07 23:18
purescriptproject-euler

Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module Euler006Test (euler6suite) where
 
import Prelude
 
import Euler006 (euler6)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
 
euler6suite :: forall e. TestSuite e
euler6suite =
  suite "Euler 6" do
    test "Warmup" do
      Assert.equal 2640 (euler6 10)
    test "Real" do
      Assert.equal 25164150 (euler6 100)

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module Euler006 (euler6) where
 
import Prelude
 
import Data.Array (range)
import Data.Foldable (sum)
import Data.Int (pow)
 
euler6 :: Int -> Int
euler6 n =
  let nums = range 1 n
      sumSq = sum (flip pow 2 <$> nums)
      sqSum = pow (sum nums) 2
  in sqSum - sumSq