icyrock.com

Home

PureScript solutions to Project Euler - problem 5

2018-Feb-21 14:00
purescriptproject-euler

Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module Euler005Test (euler5suite) where
 
import Prelude
 
import Data.BigInt (fromInt)
import Euler005 (euler5)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
 
euler5suite :: forall e. TestSuite e
euler5suite =
  suite "Euler 5" do
    test "Warmup" do
      Assert.equal (fromInt 2520) (euler5 10)
    test "Real" do
      Assert.equal (fromInt 232792560) (euler5 20)

Solution

1
2
3
4
5
6
7
8
9
10
11
12
module Euler005 (euler5) where
 
import Prelude
 
import Data.Array (foldl, range)
import Data.BigInt (BigInt, fromInt)
 
euler5 :: Int -> BigInt
euler5 n =
  let nums = range 1 n
             # map fromInt
  in foldl lcm (fromInt 1) nums