Note that there are some explanatory texts on larger screens.

plurals
  1. POPerform a modulus in a huge number?
    text
    copied!<p>I need to do a modulus operation on very large integers. The biggest integer supported by my platform (edit: .NET 2.0) is a 64 bit integer, which aren't big enough for the numbers I'm working with.</p> <p>How can I do a modulus on really big integers, like 12654875632126424875387321657498462167853687516876876?</p> <p>I have a solution that treats the number as a string and works it in pieces one by one, but I wanted to know if there's a better way.</p> <p>Here's my function treating the number as a string. It basically does long division the way you'd do it by hand.</p> <pre><code> 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 &lt; numberString.Length - 1 position += 1 curSubtraction = curSubtraction * 10 + CInt(numberString.Substring(position, 1)) If (curSubtraction / modby) &lt; 1 And position = numberString.Length - 1 Then Return curSubtraction ElseIf (curSubtraction / modby) &lt; 1 Then Continue While Else curSubtraction = curSubtraction Mod modby End If End While Return curSubtraction End Function </code></pre> <p>Is there a cleaner, more efficient way?</p> <p>EDIT: To clarify, the integers are coming from IBAN bank account numbers. According to the specification, you have to convert the IBAN account number (containing letters) into one integer. Then, you do a modulus on the integer. So, I guess you could say that the real source of the integer to perform the modulus on is a string of digits.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload