icyrock.com

Home

PureScript solution to Project Euler problem 25

2019-Oct-12 22:36
purescriptproject-euler

Problem details at Project Euler problem 25 page.

Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module Euler025Test (euler25suite) where
 
import Prelude
 
import Euler025 (euler25)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
 
euler25suite :: TestSuite
euler25suite =
  suite "Euler 25" do
    test "Warmup" do
      Assert.equal 12 (euler25 3)
    test "Real" do
      Assert.equal 4782 (euler25 1000)

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module Euler025 where
 
import Prelude
 
import Data.BigInt (BigInt, fromInt, toString)
import Data.String (length)
 
digCnt :: BigInt -> Int
digCnt = toString >>> length
 
euler25 :: Int -> Int
euler25 n =
  let go i a b
        | digCnt b == n = i
        | otherwise     = go (i + 1) b (a + b)
  in go 2 (fromInt 1) (fromInt 1)