Home

# PureScript solution to Project Euler problem 4

`2018-Jan-22 06:53`

Problem details at Project Euler problem 4 page.

## Test

 12345678910111213141516 `module` `Euler004Test (euler4suite) ``where` `import` `Prelude` `import` `Data``.``Maybe` `(``Maybe``(``..``))``import` `Euler004 (euler4)``import` `Test``.``Unit (TestSuite, suite, test)``import` `Test``.``Unit``.``Assert ``as` `Assert` `euler4suite ``::` `forall` `e``.` `TestSuite e``euler4suite ``=``  ``suite ``"Euler 4"` `do``    ``test ``"Warmup"` `do``      ``Assert``.``equal (``Just` `9009``) (euler4 ``2``)``    ``test ``"Real"` `do``      ``Assert``.``equal (``Just` `906609``) (euler4 ``3``)`

## Solution

 1234567891011121314151617181920212223242526 `module` `Euler004 (euler4, isPalindrome) ``where` `import` `Prelude` `import` `Data``.``Array` `(``filter``, range, ``reverse``)``import` `Data``.``Foldable (``maximum``)``import` `Data``.``Int` `(decimal, pow, toStringAs)``import` `Data``.``Maybe` `(``Maybe``)``import` `Data``.``String` `(fromCharArray, toCharArray)` `isPalindrome ``::` `Int` `-``> Boolean``isPalindrome n ``=``  ``let` `s ``=` `toStringAs decimal n``      ``rs ``=` `toCharArray s``           ``#` `reverse``           ``#` `fromCharArray``  ``in` `s ``==` `rs` `euler4 ``::` `Int` `-``> ``Maybe` `Int``euler4 n ``=``  ``let` `low ``=` `pow ``10` `(n ``-` `1``)``      ``high ``=` `10` `*` `low ``-` `1``      ``nums ``=` `range low high``      ``prods ``=` `(``*``) <``\$``> nums <``*``> nums``      ``palins ``=` `filter` `isPalindrome prods``  ``in` `maximum` `palins`