icyrock.com

Home

PureScript solution to Project Euler problem 36

2020-Sep-10 15:08
purescriptproject-euler

Problem details at Project Euler problem 36 page.

Test

1
2
3
4
5
6
7
8
9
10
11
12
13
module Euler036Test (euler36suite) where
 
import Prelude
 
import Euler036 (euler36)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
 
euler36suite :: TestSuite
euler36suite =
  suite "Euler 36" do
    test "Real" do
      Assert.equal 872187 (euler36 1000000)

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module Euler036 where
 
import Prelude
 
import Data.Array (filter, range, reverse, (:))
import Data.Foldable (sum)
 
digits :: Int -> Int -> Array Int
digits b n
  | n < b = [n]
  | otherwise = n `mod` b : digits b (n `div` b)
 
isPalindromicArr :: Array Int -> Boolean
isPalindromicArr a = a == reverse a
 
isPalindromic :: Int -> Boolean
isPalindromic n =
  isPalindromicArr (digits 2 n) && isPalindromicArr (digits 10 n)
 
euler36 :: Int -> Int
euler36 n = sum <<< filter isPalindromic $ range 1 (n - 1)