Created
April 28, 2019 16:08
-
-
Save lgastako/a8c6a4e1f4ea267567dc8d80f20501f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main = interact $ show . f . map (map read . words) . lines | |
f :: [[Int]] -> Int | |
f ([n]:xs:_) | |
| isArith xs = nthArith n xs | |
| otherwise = nthGeom n xs | |
isArith :: [Int] -> Bool | |
isArith (a:b:c:_) = b - a == c - b | |
nthArith :: Int -> [Int] -> Int | |
nthArith n (a:b:_) = seq !! n | |
where | |
seq = [a, a+(b-a)..] | |
nthGeom :: Int -> [Int] -> Int | |
nthGeom n (a:b:_) = seq !! n | |
where | |
seq = geoFrom a b (b `div` a) | |
geoFrom :: Int -> Int -> Int -> [Int] | |
geoFrom n n' m = seq | |
where | |
seq | |
| signum n /= signum n' = n:map (*m) seq | |
| n < n' = n:map (*m) seq | |
| otherwise = decreasing n n' | |
decreasing n n' = seq | |
where | |
seq = n:map (`div` d) seq | |
d = n `div` n' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment