Add questions 1-3
							parent
							
								
									d4cf6b25c8
								
							
						
					
					
						commit
						6bee470b8a
					
				| 
						 | 
					@ -4,6 +4,8 @@
 | 
				
			||||||
    "concat",
 | 
					    "concat",
 | 
				
			||||||
    "coprime",
 | 
					    "coprime",
 | 
				
			||||||
    "elems",
 | 
					    "elems",
 | 
				
			||||||
 | 
					    "Factorisation",
 | 
				
			||||||
 | 
					    "factorise",
 | 
				
			||||||
    "foldl"
 | 
					    "foldl"
 | 
				
			||||||
  ]
 | 
					  ]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,28 @@
 | 
				
			||||||
 | 
					{-
 | 
				
			||||||
 | 
					https://projecteuler.net/problem=1
 | 
				
			||||||
 | 
					If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
 | 
				
			||||||
 | 
					The sum of these multiples is 23.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Find the sum of all the multiples of 3 or 5 below 1000.
 | 
				
			||||||
 | 
					-}
 | 
				
			||||||
 | 
					module Main where
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					main :: IO ()
 | 
				
			||||||
 | 
					main = print ans
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ans :: Integer
 | 
				
			||||||
 | 
					ans = sum $ allNumbersUnder 1000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					isMultipleOf :: Integral a => a -> a -> Bool
 | 
				
			||||||
 | 
					isMultipleOf n x = x `rem` n == 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					all3sUnder :: Integral a => a -> [a]
 | 
				
			||||||
 | 
					all3sUnder n = filter (isMultipleOf 3) [1..n-1]
 | 
				
			||||||
 | 
					all5sUnder :: Integral a => a -> [a]
 | 
				
			||||||
 | 
					all5sUnder n = filter (isMultipleOf 5) [1..n-1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					allXsUnderN :: Integral a => a -> a -> [a]
 | 
				
			||||||
 | 
					allXsUnderN x n = filter (isMultipleOf x) [1..n-1]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					allNumbersUnder :: Integral a => a -> [a]
 | 
				
			||||||
 | 
					allNumbersUnder l = allXsUnderN 3 l <> allXsUnderN 5 l
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,16 @@
 | 
				
			||||||
 | 
					{-
 | 
				
			||||||
 | 
					https://projecteuler.net/problem=2
 | 
				
			||||||
 | 
					Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					1, 2, 3, 5, 8, 13, 21, 34, 55, 89,...
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
 | 
				
			||||||
 | 
					-}
 | 
				
			||||||
 | 
					module Main where
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					main :: IO ()
 | 
				
			||||||
 | 
					main = print ans
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ans = sum $ filter even $ takeWhile(< 4_000_000) fibs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fibs = 1 : 2 : zipWith (+) fibs (tail fibs)
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,34 @@
 | 
				
			||||||
 | 
					{-
 | 
				
			||||||
 | 
					https://projecteuler.net/problem=3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					The prime factors of 13195 are 5, 7, 13, and 29.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					What is the largest prime factor of the number 600851475143?
 | 
				
			||||||
 | 
					-}
 | 
				
			||||||
 | 
					module Main where
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import           Math.NumberTheory.Primes (Prime (unPrime),
 | 
				
			||||||
 | 
					                                           UniqueFactorisation (factorise),
 | 
				
			||||||
 | 
					                                           primes)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					main :: IO ()
 | 
				
			||||||
 | 
					main = print ans
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ans :: Int
 | 
				
			||||||
 | 
					ans = last $ filter (isFactor bigNum) primes'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					primes' :: [Int]
 | 
				
			||||||
 | 
					primes' = takeWhile (< sqrt') $ map unPrime primes
 | 
				
			||||||
 | 
					  where
 | 
				
			||||||
 | 
					    sqrt' = floor $ sqrt (toEnum bigNum)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bigNum :: Int
 | 
				
			||||||
 | 
					bigNum = 600851475143
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					isFactor :: Int -> Int -> Bool
 | 
				
			||||||
 | 
					isFactor x n = x `rem` n == 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ans' :: [Int]
 | 
				
			||||||
 | 
					ans' = map (unPrime . fst) $ factorise (fromEnum bigNum)
 | 
				
			||||||
 | 
					-- [(Prime 71,1),(Prime 839,1),(Prime 1471,1),(Prime 6857,1)]
 | 
				
			||||||
 | 
					-- [71,839,1471,6857]
 | 
				
			||||||
		Loading…
	
		Reference in New Issue