Let's start!
Function 1: mod( a , n , b)
::Return: mod(a^n,
::Example:
::-print(mod(2007,321123,332211)) -------> 285663
or 2007^321123285663 (mod 332211)
::-print(mod(2,123456789,2007)) --------> 1673
or 2^1234567891673 (mod 2007)
::-print(mod(5,200,"10^10")) --------> 2900390625
or 10 last digits of 5^200 is 2900390625 /* You must input "10^10", not 10^10! Unless it will cause an error, I dont know why (try print(10^10) in CPLua)/*
::Known bugs:none
::Author: vanhoa
::Ver: 1.0
::Code:
require("cas") function mod(a,n,c) c=cas(tostring(c)) if c==cas("0") then return "How to devide by zero?!" end if cas.abs(c)==cas.abs("1") then return cas(0) end local dec2bin=function(x) local re={} repeat if cas.mod(x,2)==cas("1") then x=cas("(x-1)/2|x="..x) re[#re+1]=1 else x=cas("x/2|x="..x) re[#re+1]=0 end until x==cas("0") return re end local pow2,bin,re={cas("mod("..a..","..c)},dec2bin(cas(tostring(n))),1 for i=2,#bin do pow2[i]=cas("mod("..pow2[i-1].."^2,"..c) end for i=1,#bin do if bin[i]==1 then re=cas("mod("..re.."*"..pow2[i]..","..c) end end return cas(re) end export{mod=mod}