From 5fa2b14842e21f3cda8503e39c92d02fc8fb47c8 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Tue, 12 Nov 2024 22:26:40 +0100 Subject: [PATCH] Add Haskell solutions for Problem 29, 41, 52 --- Haskell/ProjectEuler.hs | 6 ++++++ Haskell/p029.hs | 21 +++++++++++++++++++++ Haskell/p041.hs | 14 ++++++++++++++ Haskell/p052.hs | 12 ++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 Haskell/p029.hs create mode 100644 Haskell/p041.hs create mode 100644 Haskell/p052.hs diff --git a/Haskell/ProjectEuler.hs b/Haskell/ProjectEuler.hs index 5b5864e..1ad7517 100644 --- a/Haskell/ProjectEuler.hs +++ b/Haskell/ProjectEuler.hs @@ -3,6 +3,7 @@ module ProjectEuler , digitSum , sumProperDivisors , countDivisors +, isPandigital ) where import Data.Char (digitToInt) @@ -30,3 +31,8 @@ countDivisors :: (Integral a) => a -> Int countDivisors n = length $ nub $ concat [ [x, n `div` x] | x <- [1..limit], n `mod` x == 0 ] where limit = floor $ sqrt $ fromIntegral n +isPandigital :: Integer -> Bool +isPandigital n = n_length == length (nub n_char) && '0' `notElem` n_char && digitToInt (maximum n_char) == n_length + where n_char = show n + n_length = length n_char + diff --git a/Haskell/p029.hs b/Haskell/p029.hs new file mode 100644 index 0000000..82a23bc --- /dev/null +++ b/Haskell/p029.hs @@ -0,0 +1,21 @@ +-- Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: +-- +-- 2^2=4, 2^3=8, 2^4=16, 2^5=32 +-- 3^2=9, 3^3=27, 3^4=81, 3^5=243 +-- 4^2=16, 4^3=64, 4^4=256, 4^5=1024 +-- 5^2=25, 5^3=125, 5^4=625, 5^5=3125 +-- +-- If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms: +-- +-- 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125 +-- +-- How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100? +import Data.List + +powerCombinations :: (Integral a) => a -> [a] +powerCombinations n = nub [ x^y | x <- [2..n], y <- [2..n] ] + +main = do + let result = length $ powerCombinations 100 + putStrLn $ "Project Euler, Problem 29\n" + ++ "Answer: " ++ show result diff --git a/Haskell/p041.hs b/Haskell/p041.hs new file mode 100644 index 0000000..03e1074 --- /dev/null +++ b/Haskell/p041.hs @@ -0,0 +1,14 @@ +-- We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital +-- and is also prime. +-- +-- What is the largest n-digit pandigital prime that exists? + +import ProjectEuler (isPrime, isPandigital) + +maxPandigitalPrime :: Integer +maxPandigitalPrime = head $ filter isPrime (filter isPandigital [7654321,7654319..]) + +main = do + let result = maxPandigitalPrime + putStrLn $ "Project Euler, Problem 41\n" + ++ "Answer: " ++ show result diff --git a/Haskell/p052.hs b/Haskell/p052.hs new file mode 100644 index 0000000..42efda3 --- /dev/null +++ b/Haskell/p052.hs @@ -0,0 +1,12 @@ +-- It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order. +-- +-- Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. +import Data.List + +smallestPermutedMultiples :: Integer +smallestPermutedMultiples = head [ x | x <- [1..], sort (show x) == sort (show (2 * x)), sort (show x) == sort (show (3 * x)), sort (show x) == sort (show (4 * x)), sort (show x) == sort (show (5 * x)), sort (show x) == sort (show (6 * x)) ] + +main = do + let result = smallestPermutedMultiples + putStrLn $ "Project Euler, Problem 52\n" + ++ "Answer: " ++ show result