From 7ac9846329cc231c79fa1158f1e1f1d5fc96bc22 Mon Sep 17 00:00:00 2001 From: Mistivia Date: Thu, 24 Apr 2025 17:16:21 +0800 Subject: permutational-primes --- 4-kyu/permutational-primes.hs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 4-kyu/permutational-primes.hs diff --git a/4-kyu/permutational-primes.hs b/4-kyu/permutational-primes.hs new file mode 100644 index 0000000..6aedafe --- /dev/null +++ b/4-kyu/permutational-primes.hs @@ -0,0 +1,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 -- cgit v1.0