79443819

Date: 2025-02-16 19:48:34
Score: 1
Natty:
Report link

The original question is how to do a modulus of a very large number, but the detailed problem shows the modulus of a small number with a small exponent (which results in a very large number). These need a very different solution.

The suggested solution (function BigMod) solves the problem for modulus of a relatively small exponential number, but it can't handle one "very large number".

Excel can not store accurate numeric values of more than 15 digits (16 characters when you include the positive or negative sign) These big numbers can also cause formula errors, so you're better off calculating the modulus in another way. (the formula OP gave also gives inaccurate results for these 15 digit numbers, this might have to do with float numbers, but I won't go into detail about this)

Because of this I will show you how to solve a Modulus of a very large number (that is not an exponential number) with formulas and with a function (bottom of my answer)

OP his 2^288 example will be stored as number 497323236409787000000000000000000000000000000000000000000000000000000000000000000000000

This is an incorrect value, such a big number should be stored as a string: "497323236409786642155382248146820840100456150797347717440463976893159497012533375533056" (How you get such a big number as a string is a whole separate topic)

When you have your accurate very big number, you then have to go through the number/string from left to right, calculating modulus each time and then add the next part to it.

First possible formula solution, calculating modulus of the first chunk and adding the rest of the string back to it each time:

(note: Americans need to replace the semicolon for a comma)
A1 = "497323236409786642155382248146820840100456150797347717440463976893159497012533375533056"
(very large number)
B1 = 10
(length of the substring)
C1 = 2017
(modulus number)
A2 = MOD(LEFT(A1 ;$B$1);$C$1) & RIGHT(A1 ;LEN(A1 )-$B$1)
A3 = MOD(LEFT(A2 ;$B$1);$C$1) & RIGHT(A2 ;LEN(A2 )-$B$1)
A4 = MOD(LEFT(A3 ;$B$1);$C$1) & RIGHT(A3 ;LEN(A3 )-$B$1)
A5 = MOD(LEFT(A4 ;$B$1);$C$1) & RIGHT(A4 ;LEN(A4 )-$B$1)
A6 = MOD(LEFT(A5 ;$B$1);$C$1) & RIGHT(A5 ;LEN(A5 )-$B$1)
A7 = MOD(LEFT(A6 ;$B$1);$C$1) & RIGHT(A6 ;LEN(A6 )-$B$1)
A8 = MOD(LEFT(A7 ;$B$1);$C$1) & RIGHT(A7 ;LEN(A7 )-$B$1)
A9 = MOD(LEFT(A8 ;$B$1);$C$1) & RIGHT(A8 ;LEN(A8 )-$B$1)
A10= MOD(LEFT(A9 ;$B$1);$C$1) & RIGHT(A9 ;LEN(A9 )-$B$1)
A11= MOD(LEFT(A10;$B$1);$C$1) & RIGHT(A10;LEN(A10)-$B$1)
A12= MOD(LEFT(A11;$B$1);$C$1) & RIGHT(A11;LEN(A11)-$B$1)
A13= MOD(LEFT(A12;$B$1);$C$1) & RIGHT(A12;LEN(A12)-$B$1)
(remaining string is less than 10 characters, so we can do our final MOD now)
A14= MOD(A13;$C$1)
   = 891

Second possible formula solution, calculating the modulus of the first chunk and then keep taking the previous modulus result and adding the next chunk to it:

A2 = MOD( LEFT($A$1;$B$1);$C$1) & MID($A$1;($B$1*1)+1;$B$1)
A3 = MOD(A2;$C$1) & MID($A$1;($B$1*2)+1;$B$1)
A4 = MOD(A3;$C$1) & MID($A$1;($B$1*3)+1;$B$1)
A5 = MOD(A4;$C$1) & MID($A$1;($B$1*4)+1;$B$1)
A6 = MOD(A5;$C$1) & MID($A$1;($B$1*5)+1;$B$1)
A7 = MOD(A6;$C$1) & MID($A$1;($B$1*6)+1;$B$1)
A8 = MOD(A7;$C$1) & MID($A$1;($B$1*7)+1;$B$1)
A9 = MOD(A8;$C$1) & MID($A$1;($B$1*8)+1;$B$1)
A10= MOD(A9;$C$1)
   = 891
(with substrings of length 10 it will be done here)

If you know the maximum amount of steps you'll need, then you can combine this into 1 cell:

A2 = 
MOD(
 MOD(
  MOD(
   MOD(
    MOD(
     MOD(
      MOD(
       MOD(
        MOD(
         LEFT($A$1;$B$1)
        ;$C$1) & MID($A$1;($B$1*1)+1;$B$1)
       ;$C$1) & MID($A$1;($B$1*2)+1;$B$1)
      ;$C$1) & MID($A$1;($B$1*3)+1;$B$1)
     ;$C$1) & MID($A$1;($B$1*4)+1;$B$1)
    ;$C$1) & MID($A$1;($B$1*5)+1;$B$1)
   ;$C$1) & MID($A$1;($B$1*6)+1;$B$1)
  ;$C$1) & MID($A$1;($B$1*7)+1;$B$1)
 ;$C$1) & MID($A$1;($B$1*8)+1;$B$1)
;$C$1)
   = 891

Important note: these formulas can start throwing errors when your chunks are too big.

Now that you understand the process, I will put this into a function. This will go through the number character by character to keep it simple and to avoid errors caused by too big numbers. It also accepts both strings and numbers

Public Function BigNrMod(ByVal number As Variant, ByVal modulus As Double) As Double
    Dim remainder As Double
    remainder = 0
    Dim i As Integer
    For i = 1 To Len(number) ' Loop through each character
        'modulus of string to number (remainder & next character)
        remainder = CDbl(remainder & Mid(number, i, 1)) Mod modulus
        ' Alternatively modulus of sum (10x remainder + value of the next character)
        ' remainder = (remainder * 10 + Val(Mid(number, i, 1))) Mod modulus
    Next i
    BigNrMod = remainder
End Function

Here's also the ExponentialNumber function, but with the use of Mod instead of the formula:

Public Function ExpNrMod(ByVal number As Double, ByVal exponent As Integer, ByVal modulus As Double) As Double
    Dim tmp As Integer
    tmp = 1
    Dim i As Integer
    For i = 1 To exponent
        tmp = (tmp * number) Mod modulus
    Next i
    ExpNrMod = tmp
End Function
Reasons:
  • Blacklisted phrase (1): how to solve
  • Long answer (-1):
  • Has code block (-0.5):
  • Filler text (0.5): 000000000000000000000000000000000000000000000000000000000000000000000000
  • Low reputation (1):
Posted by: Nathan Haaren