icyrock.com

Home

PureScript solution to Project Euler problem 29

2020-Feb-02 17:45
purescriptproject-euler

Problem details at Project Euler problem 29 page.

Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module Euler029Test (euler29suite) where
 
import Prelude
 
import Euler029 (euler29)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
 
euler29suite :: TestSuite
euler29suite =
  suite "Euler 29" do
    test "Warmup" do
      Assert.equal 15 (euler29 5)
    test "Real" do
      Assert.equal 9183 (euler29 100)

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module Euler029 where
 
import Prelude
 
import Data.Array (length, nub, range)
import Data.BigInt (BigInt, fromInt, pow)
 
powers :: Int -> Array BigInt
powers n = do
  a <- range 2 n
  b <- range 2 n
  pure $ pow (fromInt a) (fromInt b)
 
euler29 :: Int -> Int
euler29 = powers >>> nub >>> length