0

i am trying to calculate: A random number in the range 10^25 mod a random number in the range of 100. I saved the first random number as a Variant because the number is too big to be saved as an Integer.

The result of the solution should be something between 1 and 100, so it could be saved as an Integer.

My idea is to break down the numbers into smaller numbers (prim factorisation) and calculate the modulo on smaller numbers, so we don't need to calculate in the range of 10^25.

For example calculation: 300400000717120000131495 mod 97 = 1 How to calc this by using VBA.

Thank you in advance. Fabian

Fabian Müller
  • 318
  • 2
  • 11
  • What problem are you trying to solve here? If the end result is `a mod b` where b <= 100 and both numbers are random, does it matter how large `a` is? – jsheeran Jan 04 '23 at 11:05

1 Answers1

4

Does this work for you?

Sub test_this()
    Debug.Print find_modulus("300400000717120000131495", 97)
End Sub

Function find_modulus(large_number As String, divisor As Long)
    Dim quotient As Variant, integer_quotient As Variant
    quotient = CDec(large_number)
    integer_quotient = Int(quotient / divisor) * divisor
    find_modulus = quotient - integer_quotient
End Function

(Variable names changed to make more sense.)

CLR
  • 11,284
  • 1
  • 11
  • 29
  • Thanks, tried it like you, but I must have done something wrong :-( – Fabian Müller Jan 04 '23 at 11:12
  • `start_value / modulo` will overflow `Integer`, or even `Long`, for a value this large. – jsheeran Jan 04 '23 at 11:12
  • 2
    @jsheeran - that's why it's a variant/Dec. The Int function doesn't (it seems) return an Integer type when given a Dec type. – CLR Jan 04 '23 at 11:14
  • Nice! Voted up... The maximum accepted by `Mod` (not documented AFAIK...) looks to be the `LongLong` type (`9223372036854775807^`)... – FaneDuru Jan 04 '23 at 11:39