Add Haskell solutions for Problem 29, 41, 52
This commit is contained in:
parent
27a7e873c1
commit
5fa2b14842
@ -3,6 +3,7 @@ module ProjectEuler
|
|||||||
, digitSum
|
, digitSum
|
||||||
, sumProperDivisors
|
, sumProperDivisors
|
||||||
, countDivisors
|
, countDivisors
|
||||||
|
, isPandigital
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Char (digitToInt)
|
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 ]
|
countDivisors n = length $ nub $ concat [ [x, n `div` x] | x <- [1..limit], n `mod` x == 0 ]
|
||||||
where limit = floor $ sqrt $ fromIntegral n
|
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
|
||||||
|
|
||||||
|
21
Haskell/p029.hs
Normal file
21
Haskell/p029.hs
Normal file
@ -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
|
14
Haskell/p041.hs
Normal file
14
Haskell/p041.hs
Normal file
@ -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
|
12
Haskell/p052.hs
Normal file
12
Haskell/p052.hs
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user