Home

# PureScript solution to Project Euler problem 37

`2020-Oct-05 13:36`

Problem details at Project Euler problem 37 page.

## Test

 12345678910111213 `module` `Euler037Test (euler37suite) ``where` `import` `Prelude` `import` `Euler037 (euler37)``import` `Test``.``Unit (TestSuite, suite, test)``import` `Test``.``Unit``.``Assert ``as` `Assert` `euler37suite ``::` `TestSuite``euler37suite ``=``  ``suite ``"Euler 37"` `do``    ``test ``"Real"` `do``      ``Assert``.``equal ``748317` `(euler37 unit)`

## Solution

 1234567891011121314151617181920212223242526272829303132333435363738394041 `module` `Euler037 ``where` `import` `Prelude` `import` `Data``.``Array` `(``all``, ``concat``, ``concatMap``, ``filter``, (``:``))``import` `Data``.``Foldable (``sum``)` `isPrime ``::` `Int` `-``> Boolean``isPrime n ``=``  ``let` `go j``        ``|` `j ``*` `j > n      ``=` `true``        ``|` `n ```mod`` `j ``==` `0` `=` `false``        ``|` `otherwise`      `=` `go (j ``+` `1``)``  ``in` `n > ``1` `&& go ``2` `rtruncs ``::` `Unit ``-``> ``Array` `Int``rtruncs ``_` `=``  ``let` `radd l ``=` `(``\``j ``-``> l ``*` `10` `+` `j) <``\$``> [``1``, ``3``, ``7``, ``9``]``      ``rps ls ``=` `filter` `isPrime ``\$` `concatMap` `radd ls``      ``go acc ls ``=``        ``case` `rps ls ``of``          ``[] ``-``> acc``          ``ps ``-``> go (``concat` `[acc, ps]) ps``  ``in` `go [] [``2``, ``3``, ``5``, ``7``]` `ltruncs ``::` `Int` `-``> ``Array` `Int``ltruncs j ``=``  ``let` `go acc k m ``=``        ``case` `k ```mod`` `m ``of``          ``0` `-``> acc``          ``l ``-``> go (l ``:` `acc) k (m ``*` `10``)``  ``in` `go [] j ``10` `pltrunc ``::` `Int` `-``> Boolean``pltrunc ``=` `all` `isPrime <<< ltruncs` `gen ``::` `Unit ``-``> ``Array` `Int``gen ``_` `=` `(``filter` `pltrunc <<< rtruncs) unit` `euler37 ``::` `Unit ``-``> ``Int``euler37 ``=` `sum` `<<< gen`