Euler questions 5/6
parent
fc26e6e6a9
commit
a0fef78e65
|
@ -0,0 +1,65 @@
|
|||
import Data.Set (fromList)
|
||||
import Debug.Trace (trace)
|
||||
|
||||
{-
|
||||
https://projecteuler.net/problem=5
|
||||
|
||||
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
|
||||
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
||||
-}
|
||||
|
||||
wrong = map (`div` 2520) [1..10]
|
||||
-- [0,0,0,0,0,0,0,0,0,0]
|
||||
|
||||
_2520misleadingSolution = map (2520 `div`) [1..10]
|
||||
-- [2520,1260,840,630,504,420,360,315,280,252]
|
||||
|
||||
_2520correctSolution = map (2520 /) [1..10]
|
||||
-- [2520.0,1260.0,840.0,630.0,504.0,420.0,360.0,315.0,280.0,252.0]
|
||||
|
||||
-- sieve method
|
||||
-- zip 20 lists together
|
||||
|
||||
even' n = n `rem` 2 == 0
|
||||
odd' = not . even'
|
||||
|
||||
multipleOf x n = {- trace ("x:" <> show x <> ", n:" <> show n) $ -} n `rem` x == 0
|
||||
|
||||
threven = multipleOf 3
|
||||
fouevn = multipleOf 4
|
||||
fiven = multipleOf 5
|
||||
|
||||
{-
|
||||
filter even [1..100]
|
||||
filter fouevn $ filter threven $ filter even [1..100]
|
||||
|
||||
-- :t foldl (\b a -> undefined) [] [1..]
|
||||
foldl (\b a -> undefined) [] [1..]
|
||||
-}
|
||||
|
||||
bruteForce =
|
||||
filter (multipleOf 20) $
|
||||
filter (multipleOf 19) $
|
||||
filter (multipleOf 18) $
|
||||
filter (multipleOf 17) $
|
||||
filter (multipleOf 16) $
|
||||
filter (multipleOf 15) $
|
||||
filter (multipleOf 14) $
|
||||
filter (multipleOf 13) $
|
||||
filter (multipleOf 12) $
|
||||
filter (multipleOf 11) $
|
||||
filter (multipleOf 10) $
|
||||
filter (multipleOf 9) $
|
||||
filter (multipleOf 8) $
|
||||
filter (multipleOf 7) $
|
||||
filter (multipleOf 6) $
|
||||
filter (multipleOf 5) $
|
||||
filter (multipleOf 4) $
|
||||
filter (multipleOf 3) $
|
||||
filter (multipleOf 2) [232792550..]
|
||||
-- answer is 232792560
|
||||
|
||||
test = map (filter . multipleOf) [1..10]
|
||||
|
||||
-- lol
|
||||
solve = foldl1 lcm [1..20]
|
|
@ -0,0 +1,28 @@
|
|||
{-
|
||||
The sum of the squares of the first ten natural numbers is,
|
||||
1^2 + 2^2 + ... + 10^2 = 385
|
||||
|
||||
The square of the sum of the first ten natural numbers is,
|
||||
(1 + 2 + ... + 10)^2 = 55^2 = 3025
|
||||
|
||||
Hence the difference between the sum of the squares
|
||||
of the first ten natural numbers and the square
|
||||
of the sum is 3025 - 385 = 2640
|
||||
|
||||
Find the difference between the sum of the squares
|
||||
of the first one hundred natural numbers
|
||||
and the square of the sum.
|
||||
-}
|
||||
upperRange = 100
|
||||
square n = n^2
|
||||
squares = map square [1..upperRange]
|
||||
sum' = go 0
|
||||
where
|
||||
go acc [] = acc
|
||||
go acc (x:xs) = go (acc+x) xs
|
||||
|
||||
sumOfSquares = sum' squares
|
||||
|
||||
squareOfTheSum = (sum [1..upperRange])^2
|
||||
|
||||
solution = squareOfTheSum - sumOfSquares
|
Loading…
Reference in New Issue