javascript,math,hex,modular-arithmetic

In the simplest case, you can add one digit at a time, keeping track of carry: var ndigits = 4, i, carry = 0, d, result = ""; for (i = ndigits - 1; i >= 0; i--) { d = parseInt(a[i], 16) + parseInt(b[i], 16) + carry; carry =...

java,biginteger,number-theory,modular-arithmetic

Flustered this was put on hold... For the sake of theory, if you wanted to write your own custom method please check the following out, based on a math trick to avoid the computation. First the solution, and then the math behind it. Your subroutine could look something like the...

c++,performance,number-theory,integer-arithmetic,modular-arithmetic

You can satisfy for one i at a time while preserving all the ones already satisfied: step is the LCM of all the a[i] already satisfied (initially 1). y is the minimum value satisfying all the ones you did already. The key concept is that any Y satisfying all i...

python,math,modular-arithmetic,ceil

Assuming the indentation issue is not a real issue, the issue is that your numbers can reach below 1 when going recursively, and then once it reaches below 1 (that is n reaches 0 ) , it keeps on calling SBSeq recursively without exiting. The condition in the start of...

math,rsa,public-key-encryption,modular-arithmetic

In g^x = 1, the solution x will always be a divider of (p-1)*(q-1). Choose some different g values, and you will likely find most factors of (p-1)*(q-1). And as (p-1)(q-1) = N - p - q + 1, knowing (p-1)(q-1) and N results in knowing p + q. Knowing...

This is called the The Discrete Logarithm Problem. It is a difficult problem---computationally intensive. See the Wikipedia page. It has discussion of eight algorithms for the problem. The Maple command is numtheory:-mlog. numtheory:-mlog(82, 10, 543); Maple responds 12 To obtain all such integers j, use the extended form of the...

modulus,number-theory,modular-arithmetic

There two important theories that would help you to solve this problem :- Modular Exponentiation Fermat's little theorem Modular Exponentiation :- This simple recursive formula to make you understand :- (a^n)%p = ((a^(n-1))%p*a)%p The above formula can help you prevent the overflow which occurs if a^n is large. Moreover fast...

c,random,primes,modular-arithmetic

The following if statement if (x1 > r) x1 += r + 2; should be written as if (x1 > r) x1 -= r; Both results are the same modulo 2^31: x1 + r + 2 = x1 + 2^31 - 1 + 2 = x1 + 2^31 + 1...

Some time ago i used GMP library, maybe it will help you too. https://gmplib.org/ This should be a comment but can`t make one yet....

language-agnostic,rsa,number-theory,modular-arithmetic

Well, sort of... Suppose that you have determined the number "d" such that d*e=1 (mod phi(n)) Where phi(n) is the size of the set of relatively prime numbers relative to n. For example, if n=pq where p and q are prime, then phi(n)=(p-1)*(q-1). Then m=c^d (mod n) In the case...

Since the question is about modular inverse, I suppose it will benefit the seeker to look at another SO question. Crux of the answer is - Net 4.0+ implements BigInteger with a special modular arithmetics function ModPow (which produces “X power Y modulo Z”), you don't need a third-party library...

c++,algorithm,recursion,exponentiation,modular-arithmetic

i think the usage of int res instead of uii res is the problem there are chances of overflow. Moreove even ((res*res)*base%p)%p can cause overflow . Improved code :- uii modularExponentiation(uii base,uii exponent,uii p) { if(exponent == 0) return 1; uii res= modularExponentiation(base,exponent/2,p); res = (res*res)%p; if(exponent%2 == 0) return...

performance,cryptography,biginteger,diffie-hellman,modular-arithmetic

64 bit CPUs are significantly faster at big integer arithmetic than 32 bit CPUs. My experience is a factor 2 with identical code and a factor 4 with specialized code. In code written with x86 in mind many intermediate values have 64 bits. For example if you multiply two 32...