# User talk:PaoloMartini

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

$p(x)=\sum _{i=0}^{n}a_{i}x^{i},a_{n}\neq 0$

$p(x)=a_{n}x^{n}+\sum _{i=0}^{n-1}a_{i}x^{i}$

$p(x-1)=a_{n}(x-1)^{n}+\sum _{i=0}^{n-1}a_{i}(x-1)^{i}$

$(x-1)^{n}=\sum _{k=0}^{n}{n \choose k}x^{n-k}(-1)^{k}$

$p(x-1)=a_{n}\sum _{k=0}^{n}{n \choose k}x^{n-k}(-1)^{k}+\sum _{i=0}^{n-1}a_{i}(x-1)^{i}$

$p(x-1)=a_{n}x^{n}+a_{n}\sum _{k=1}^{n}{n \choose k}x^{n-k}(-1)^{k}+\sum _{i=0}^{n-1}a_{i}(x-1)^{i}$

{-# OPTIONS_GHC -fglasgow-exts #-}
module Polynomial where

import Test.QuickCheck

-- *f*actorial
f :: Integer -> Integer
f n = product [1..n]

-- *c*hoose -- binomial coefficient
c :: Integer -> Integer -> Integer
n c k = f n div (f k * f (n-k))

-- *p*olynomial
p, q :: [Integer] -> Integer -> Integer
p a x = sum [(a!!fromIntegral i)*(x^i) | i <- [0..n]] where n = length a - 1

q a x = ((a!!n) * ((x+1)^n))
+ ((a!!n) * sum [(fromIntegral n c fromIntegral k)*((x+1)^(n-k))*((-1)^k) | k <- [1..n]])
+ sum [(a!!fromIntegral i)*(x^i) | i <- [0..n-1]] where n = length a - 1

-- *t*est
t = quickCheck \$ \(x::Integer) (xs::[Integer]) ->
not (null xs) && last xs /= 0  ==>  p xs x == q xs x