Home > Net >  Modulus of a large number
Modulus of a large number

Time:02-05

I am trying to calculate the modulus of a very large number (> 38 digits) in T-SQL.

I used to cast my variable as a numeric but now that the number is too large, it is throwing me an error : Arithmetic overflow error converting varchar to data type numeric.

For example, I would like to know how I should proceed to get the result of this : 17448000012524221015281629272289115277 % 97 (result should be 1 in that case)

Thanks for your help.

CodePudding user response:

You can compute modulus by splitting the long integer into parts based on modulo properties:

(a b) mod n = [(a mod n) (b mod n)] mod n.

and

ab mod n = [(a mod n)(b mod n)] mod n.

17448000012524221015281629272289115277 = 17448000012524221015281629 * 1000000000000   272289115277

so

select ((17448000012524221015281629 % 97)*(1000000000000 % 97)   272289115277 % 97) % 97 as modulo

CodePudding user response:

Here's solution that, when the number is too big, will cut it up to calculate the modulus 97.

The thing is that every 97 by a power of 10 will have 0 as modulus 97

    970           
  •  Tags:  
  • Related