I understand that in order for this function to work crtHasSolution has to be true first I'm having trouble proving that n could be a solution any ideas or tips in how to write or check that in haskell?
I know that the conditions for N are that it has to be bigger or equal to 0 and smaller then m which is the product of all the mod bases.
crtHasSolution :: [Integer] -> [Integer] -> Bool
crtHasSolution as ms = length as > 0 &&
length ms > 0 &&
length as == length ms &&
all (>=0) as &&
all (>=2) ms &&
pairwise_coprime ms
-- Is a given number a solution to a CRT problem?
-- usage: crtIsSolution n as ms = ans
-- assures: ans == True, if crtHasSolution as ms == True and n is a solution
-- ans == False, otherwise
crtIsSolution :: Integer -> [Integer] -> [Integer] -> Bool
crtIsSolution n as ms = crtHasSolution as ms &&
n >= 0 &&
n < m
where m =
From wikipedia:
If
x `mod` m_i /= a_i
for anyi
, thenx
is not a solution.This smacks of homework, so rather than give you a one-liner that computes this, I'm going to give you some leading questions for your implementation of
crtIsSolution n as ms
:n
a solution ifms
andas
are empty?n `mod` m_0 == a_0
and whethern
is a solution forms_0
andas_0
, can you compute whethern
is a solution form_0:ms_0
anda_0:as_0
?