From f5d5a26d4b9796a0f8ffb6c06101e56ead2b8490 Mon Sep 17 00:00:00 2001 From: Jesse McDonald Date: Sat, 22 Aug 2015 19:39:42 -0500 Subject: [PATCH] Add solution for problem 51. --- Problem051.hs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Problem051.hs diff --git a/Problem051.hs b/Problem051.hs new file mode 100644 index 0000000..2d317b6 --- /dev/null +++ b/Problem051.hs @@ -0,0 +1,30 @@ +-- By replacing the 1st digit of the 2-digit number *3, it turns out that six +-- of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime. +-- +-- By replacing the 3rd and 4th digits of 56**3 with the same digit, this +-- 5-digit number is the first example having seven primes among the ten +-- generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, +-- 56773, and 56993. Consequently 56003, being the first member of this family, +-- is the smallest prime with this property. +-- +-- Find the smallest prime which, by replacing part of the number (not +-- necessarily adjacent digits) with the same digit, is part of an eight prime +-- value family. + +import Euler +import Debug.Trace + +isPrime :: Integer -> Bool +isPrime n = n >= 2 && all (\p -> n `mod` p /= 0) smallPrimes + where smallPrimes = takeWhile (<= floor (sqrt (fromIntegral n))) primes + +main = print $ head $ do + base <- primes + let digits = toDigits base + template <- sequenceA <$> mapM (\n -> [const n, id]) digits + let family = nub $ filter isPrime $ map fromDigits $ + filter ((/= 0) . head) $ map template [0..9] + guard $ take 1 family == [base] + guard $ length family >= 8 + traceM $ show family + return base