using System.Diagnostics;
public static int ModuloBitShiftRussianPeasant(int i, int j)
public static int ModuloDecimalMath(int i, int j)
decimal decresult = i/(decimal)j;
return (int)(((decresult - (int)decresult)*j)+0.000000000000000000000000003M);
public static int ModuloMathString(int i, int j)
string decresult = (i/(decimal)j).ToString();
int idxperiod = decresult.IndexOf('.');
string denominator = decresult.Substring(idxperiod);
i = (int)(j*(Convert.ToDecimal(denominator)+0.000000000000000000000000003M));
i = Convert.ToInt32(decresult);
public static int ModuloMathAdditive(int i, int j)
for (int x = 0; x < i; x++)
if ((total + y) >= i) return y;
public static void Main()
Stopwatch sw = new Stopwatch();
Console.WriteLine("{0} % {1} mod={2}",number,div,mod);
Console.WriteLine(sw.ElapsedTicks+" ticks");
mod = ModuloBitShiftRussianPeasant(number, div);
Console.WriteLine("ModuloBitShiftRussianPeasant mod="+mod);
Console.WriteLine(sw.ElapsedTicks+" ticks");
mod = ModuloDecimalMath(number, div);
Console.WriteLine("ModuloDecimalMath mod="+mod);
Console.WriteLine(sw.ElapsedTicks+" ticks");
mod = ModuloMathString(number, div);
Console.WriteLine("ModuloMathString mod="+mod);
Console.WriteLine(sw.ElapsedTicks+" ticks");
mod = ModuloMathAdditive(number, div);
Console.WriteLine("ModuloMathAddative mod="+mod);
Console.WriteLine(sw.ElapsedTicks+" ticks");