Untitled

 avatar
unknown
plain_text
5 months ago
1.8 kB
4
Indexable
import Data.List (nub)

-- Convert a list of digits to a number
number :: [Int] -> Int
number [] = 0
number (x:xs) = x * 10 ^ length xs + number xs 

-- Check if a list of digits represents a prime number
isPrime :: Int -> Bool
isPrime n
    | n < 2     = False
    | otherwise = null [x | x <- [2..isqrt n], n `mod` x == 0]
  where
    isqrt = floor . sqrt . fromIntegral

-- Check if the sum of the digits of a number is odd
oddSum :: [Int] -> Bool
oddSum xs = odd . sum $ xs

-- Generate all prime numbers with three digits and odd digit sum
primes :: [[Int]]
primes = [[a, b, c] | a <- [1..9], b <- [0..9], c <- [0..9], 
                        a /= b, b /= c, a /= c, 
                        oddSum [a, b, c], 
                        isPrime (number [a, b, c])]

-- Generate all possible tuples of lists of integers (S1, S2, S3) with unique digits
generator1 :: [([Int], [Int], [Int])]
generator1 = [(s1, s2, s3) | s1 <- primes, s2 <- primes, s3 <- primes, 
                                  length (nub (s1 ++ s2 ++ s3)) == 9, 
                                  let [n1, n2, n3] = map number [s1, s2, s3] 
                                  in isEquallySpaced n1 n2 n3]

-- Check if three lists form equally spaced numbers
isEquallySpaced :: Int -> Int -> Int -> Bool
isEquallySpaced a b c = (b - a) == (c - b)

-- Validate the combination using all checks at once
selector1 :: ([Int], [Int], [Int]) -> Bool
selector1 (s1, s2, s3) = 
  let [n1, n2, n3] = map number [s1, s2, s3]
  in  oddSum s1 && oddSum s2 && oddSum s3 &&
      isPrime n1 && isPrime n2 && isPrime n3 &&
      isEquallySpaced n1 n2 n3

-- Main function to print solutions
main :: IO ()
main = print $ filter selector1 generator1
Editor is loading...
Leave a Comment