-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP101.hs
33 lines (23 loc) · 1007 Bytes
/
P101.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Data.Ratio
-- 7 -> [2,4] -> a quadratic polynomial through the points (7,1), (2,0), (4,0)
lagrangeBasisPolynomial :: Fractional a => a -> [a] -> a -> a
lagrangeBasisPolynomial y zs x = product [(x - z) / (y - z) | z <- zs]
lagrangeInterpolationPolynomial :: (Fractional a, Eq a) => [(a,a)] -> a -> a
lagrangeInterpolationPolynomial points z =
sum [y * lagrangeBasisPolynomial x (map fst (filter (/=(x,y)) points)) z |
(x,y) <- points]
type Rat = Ratio Integer
u :: Rat -> Rat
u n = 1 - n + n^2 - n^3 + n^4 - n^5 + n^6 - n^7 + n^8 - n^9 + n^10
points :: [(Rat,Rat)]
points = [(x,u x) | x <- [1..]]
bops :: [Rat -> Rat]
bops = map lagrangeInterpolationPolynomial [take k points | k <- [1..10]]
fit :: (Rat -> Rat) -> Rat
fit bop = fst . head . filter (uncurry (/=)) $ [(bop x, u x) | x <- [1..]]
answer :: Rat
answer = sum . map fit $ bops
main :: IO ()
main = do case denominator answer of
1 -> print $ numerator answer
_ -> error "answer is not an integer"