Метод DivRem
BigInteger.DivRem принимает делимое
и делитель, записывает остаток в
out BigInteger remainder и возвращает
частное. Оба результата имеют тип
BigInteger.
Так за один вызов получают и частное,
и остаток от деления.
Синтаксис
BigInteger quotient = BigInteger.DivRem(dividend, divisor, out BigInteger remainder);
Пример
Давайте разделим 10 на 3:
using System;
using System.Numerics;
class Program
{
static void Main()
{
BigInteger q = BigInteger.DivRem(10, 3, out BigInteger rem);
Console.WriteLine(q + " " + rem);
}
}
Результат выполнения кода:
3 1
Пример
Давайте разделим большие значения:
using System;
using System.Numerics;
class Program
{
static void Main()
{
BigInteger dividend = BigInteger.Parse("100000000000000000000");
BigInteger divisor = BigInteger.Parse("99999999999999999999");
BigInteger q = BigInteger.DivRem(dividend, divisor, out BigInteger rem);
Console.WriteLine(q + " " + rem);
}
}
Результат выполнения кода:
1 1
Пример
Давайте получим нулевой остаток:
using System;
using System.Numerics;
class Program
{
static void Main()
{
BigInteger q = BigInteger.DivRem(12345, 5, out BigInteger rem);
Console.WriteLine(rem);
}
}
Результат выполнения кода:
0
Смотрите также
-
GreatestCommonDivisor,
который ищет наибольший общий делитель -
Pow,
который умножает число само на себя -
Sign,
которое показывает знак при делении -
Int64,
из которого задают операнды неявно