icyrock.com

Home

PureScript solution to Project Euler problem 34

2020-Jul-03 18:23
purescriptproject-euler

Problem details at Project Euler problem 34 page.

Test

1
2
3
4
5
6
7
8
9
10
11
12
13
module Euler034Test (euler34suite) where
 
import Prelude
 
import Euler034 (euler34)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
 
euler34suite :: TestSuite
euler34suite =
  suite "Euler 34" do
    test "Real" do
      Assert.equal 40730 (euler34 unit)

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module Euler034 where
 
import Prelude
 
import Data.Array (filter, range, (:))
import Data.Foldable (sum)
 
fact :: Int -> Int
fact 0 = 1
fact j = j * fact (j - 1)
 
digits :: Int -> Array Int
digits j
  | 0 <= j && j <= 9 = [j]
  | otherwise   = j `mod` 10 : digits (j `div` 10)
 
curious :: Int -> Boolean
curious j = (sum <<< map fact <<< digits) j == j
 
euler34 :: Unit -> Int
euler34 _ = (sum <<< filter curious) (range 10 100000)