blob: 6aedafe0743747f0d7347418e4d945de73bbd2dc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
-- https://www.codewars.com/kata/55eec0ee00ae4a8fa0000075
module PermutationalPrimes (permutationalPrimes) where
import Control.Monad
import qualified Data.List as L
import qualified Data.Set as Set
deduplicateList :: Ord a => [a] -> [a]
deduplicateList = Set.toList . Set.fromList
permutations :: Int -> Int -> [Int]
permutations n upperLimit =
filter (<= upperLimit) $
map read $ filter (\lst -> head lst /= '0') $
lstPermutations $ show n
lstPermutations = L.permutations
primes = filterPrime [2..] where
filterPrime (p:xs) =
p : filterPrime [x | x <- xs, x `mod` p /= 0]
permuatationsPrimeCnt primeSet n upperLimit =
if foldl1 min permPrimes == n then (length permPrimes - 1)
else 0
where
permPrimes = deduplicateList $ filter isPrime $ permutations n upperLimit
where isPrime x = Set.member x primeSet
permutationalPrimes :: Int -> Int -> Maybe (Int, Int, Int)
permutationalPrimes nMax kPerms =
if ret /= [] then
Just (length ret, head ret, foldl1 max ret)
else
Nothing
where
ret = seq primeSet $ filter (\n -> kPerms == permuatationsPrimeCnt primeSet n nMax) $
Set.toList primeSet
where primeSet = Set.fromList $ takeWhile (<=nMax) primes
|