Home

PureScript solution to Project Euler problem 48

2021-Sep-30 21:21
purescriptproject-euler

Problem details at Project Euler problem 48 page.

Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module Euler048Test (euler48suite) where
 
import Prelude
 
import Euler048 (euler48)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
 
euler48suite :: TestSuite
euler48suite =
  suite "Euler 48" do
    test "Warmup" do
      Assert.equal "0405071317" (euler48 10)
 
    test "Real" do
      Assert.equal "9110846700" (euler48 1000)

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module Euler048 where
 
import Prelude
 
import Data.Array (range)
import Data.BigInt as BI
import Data.Foldable (sum)
import Data.String (drop, length)
 
euler48 :: Int -> String
euler48 =
  let f j = BI.pow j j
      g s = drop (length s - 10) s
  in g <<< BI.toString <<< sum <<< map f <<< map BI.fromInt <<< range 1