solved wilson based and fermat's based algo