public static BigInt Mod(BigInt a1, BigInt b) // деление по модулю { var a = new BigInt('+', new List <int>(a1.number)); a.DeleteNuls(); b.DeleteNuls(); if (a < b) { return(a); } else if (a == b) { return(Zero); } else { while (a > b) { var bSDopisNuls = new BigInt(b.sign, new List <int>(b.number)); BigInt newraz; var razn = new BigInt((a.number.Count - b.number.Count).ToString());//1 bSDopisNuls *= new BigInt("10").Pow(razn); if (bSDopisNuls > a) { newraz = razn - One;//2 } else { newraz = razn; } var rabRazr = new BigInt("10").Pow(newraz);//3 var newrazRes = b * rabRazr; for (var i = 1; b *rabRazr.MultOnDigit(i) <= a; ++i) { var r = rabRazr.MultOnDigit(i); newrazRes = r; }//4 if (a >= newrazRes * b) { if (a - newrazRes * b < b) { a -= newrazRes * b; break; } } else { break; } a -= newrazRes * b; } a.DeleteNuls(); return(a); } }
public static BigInt operator *(BigInt a, BigInt b) { BigInt temp = new BigInt(a), sum = new BigInt(); for (int i = 0; i < b.number.Count; i++) { temp = temp.MultOnDigit(b.number[i]); temp = temp.MultOn10Pow(b.number.Count - i - 1); sum = sum + temp; temp = new BigInt(a); } if (sum != new BigInt(0)) { sum.Sign = a.Sign * b.Sign; } else { sum.Sign = 1; } return(sum); }
public static BigInt Div(BigInt a1, BigInt b) // деление нацело { var a = new BigInt('+', new List <int>(a1.number)); a.DeleteNuls(); b.DeleteNuls(); if (a < b) { return(Zero); } else if (a == b) { return(One); } else { var res = new List <BigInt>(); while (a > b) { var r = new BigInt(b.sign, new List <int>(b.number)); var bSDopisNuls = new BigInt(b.sign, new List <int>(b.number)); var newrazRes = Zero; var newraz = 0; var razn = a.number.Count - b.number.Count;//1 bSDopisNuls = bSDopisNuls.MultOn10(razn); if (bSDopisNuls > a) { newraz = razn - 1;//2 } else { newraz = razn; } var rabRazr = new BigInt('+' + (Math.Pow(10, newraz)).ToString());//3 for (var i = 1; b *rabRazr.MultOnDigit(i) < a; i++) { r = rabRazr.MultOnDigit(i); newrazRes = r; }//4 if (a > newrazRes * b) { if (a - newrazRes * b < b) { res.Add(r); break; } } else { break; } a -= newrazRes * b; res.Add(r); } var result = Zero; foreach (var e in res) { result += e; } result.sign = a.sign == b.sign ? '+' : '-'; return(result); } }