Run the module in huge numbers?

I need to perform a module operation on very large integers. The largest integer supported by my platform (edit: .NET 2.0) is a 64-bit integer that is not large enough for the numbers I'm working with.

How can I make a module on really large integers, for example 12654875632126424875387321657498462167853687516876876?

I have a solution that treats a number as a string and processes it piecemeal one by one, but I wanted to know if there is a better way.

Here, my function treats the number as a string. This basically makes a long split the way you did it manually.

    Public Function MyMod(ByVal numberString As String, ByVal modby As Integer) As Integer
        Dim position As Integer = -1
        Dim curSubtraction As Integer = 0

        While position < numberString.Length - 1
            position += 1
            curSubtraction = curSubtraction * 10 + CInt(numberString.Substring(position, 1))

            If (curSubtraction / modby) < 1 And position = numberString.Length - 1 Then
                Return curSubtraction
            ElseIf (curSubtraction / modby) < 1 Then
                Continue While
            Else
                curSubtraction = curSubtraction Mod modby
            End If
        End While
        Return curSubtraction
    End Function

Is there a cleaner, more efficient way?

EDIT: , IBAN. , IBAN ( ) . . , , , on - .

+5
4

, , . , , :

(a + b) MOD n = ((a MOD n) + (b MOD n)) MOD n

ab MOD n = (a MOD n)(b MOD n) MOD n
+5

/ . Google bignum.

+3

, IntX.

0

If you are using .NET 4, you can simply use BigInteger. But here's how to do it with earlier versions.

There is a mathematical trick with mods in which you can simply take the first number of digits, calculate the mod at that value, then add the result of this mod back to the remaining digits and continue repeating the process until you reach the end of your “huge” number.

Bring a recursive method! (Sorry, I do not do VB)

private static int Mod(string value, int mod) {
    if (string.IsNullOrEmpty(value)) throw new ArgumentException("Invalid value.", "value");
    if (mod <= 0) throw new ArgumentException("Invalid mod.", "mod");

    int maxLength = long.MaxValue.ToString().Length - 1;

    return value.Length > maxLength
        ? Mod((Convert.ToInt64(value.Substring(0, maxLength)) % mod).ToString() + value.Substring(maxLength), mod)
        : Convert.ToInt32(Convert.ToInt64(value) % mod);}
0
source

All Articles