1

I have the following modular equation:

327≡ ℎ*327*327* ≡ ℎ*327 ≡ 1 (mod 1009) and so

 ≡ ℎ*327x ≡ ℎ*1 ≡ ℎ (mod 1009).

So I have to find out what ℎ is.

3*327=981≡-28(mod1009)

What i don't understand is how the 3 is derived here, by what formula do you use to find the 3.

I know that -28%1009 = 981, but what i don't know is how the author derived knowing to multiply by 3 to get an answer that creates the mod that gets the number. Any help here would be appreciated

The full formula is here, i know that this can be solved with EGCD, but i'm trying to understand how the author solved the solution the way he did:

The question:

In [1505]: (327*327*108)%1009                                                                                                                                 
Out[1505]: 327
Only 108 (as far as i know( will return 327. So the equation is:

(327*327*x)%1009 = 327.  What is the quickest way to solve for this step by step. 
 ( What i don't understand is how the author solved for the 3 here in the step listed below ( 3*327 =981 ≡−28 (mod 1009) )

The Answer ( which worked )
gcd(327,1009)=1 so there is an ℎ so that 327∗ℎ≡1(mod1009) so if 327*327*≡327(mod1009) then

327≡ℎ*327*327*≡ℎ*327≡1(mod1009) and so

≡ℎ*327≡ℎ*1≡ℎ(mod1009).

So I have to find out what ℎ is.

3 * 327 = 981 ≡ −28 (mod 1009)

327=28 * 12 − 9 so

327≡(−3 * 327) * 12 − 9 ( mod 1009 )

378 * 327≡−9 (mod 1009)

28=3 * 9 + 1

-3 * 327 ≡ 3 * (-37 * 327)+1 (mod 1009)

108* 327≡1 (mod 1009)

So ℎ≡108 and ≡108 (mod 1009).

=====

And indeed 108* 327 *327=35316 *327= (1009 *35+1) *327=1009 *(35 *327)+327.

Ok based on the responses, i came up with this program which seems to verify the results:

def getmod4(A, N):
  multiplier = N//A
  a = A * multiplier
  b = -(N - a)
  va = b%N
  assert va == a

  # or, the next e,d can be done by a // b ??
  e = a // b #math.gcd((va|1)-1, (b|1)-1)
  #f = -(va//e)

  c = math.gcd((A|1)-1, (b|1)-1)
  c = abs(b)//c - c

  d = abs(b) * c - A

  assert ((-multiplier * A) * c - d)%N == A

  print(f"Equation: {abs(b)} * {c} - {d}:  {abs(b) * c - d}") 
  print(f"Verification: ((-{multiplier}*{A}) * {c} - {d})%{N} = {((-multiplier*A) * c - d)%N}")
  return abs(b) * c - d

result:

In [3079]: getmod4(327,1009)                                                                                                                                  
Equation: 28 * 12 - 9:  327
Verification: ((-3*327) * 12 - 9)%1009 = 327
Out[3079]: 327

In [3081]: getmod4(261,10099)                                                                                                                                 
Equation: 181 * -20 - -3881:  261
Verification: ((-38*261) * -20 - -3881)%10099 = 261
Out[3081]: 261
Joni
  • 108,737
  • 14
  • 143
  • 193
oppressionslayer
  • 6,942
  • 2
  • 7
  • 24
  • I don't follow the steps you posted. But, since `gcd(327, 1009) == 1`, there exist integers `a, b` such that `327*a + 1009*b = 1`, for example `a = 108`, `b = -35`. Then multiply the original equation by `108` and find `x`. – dxiv Aug 09 '20 at 02:01
  • @dxiv, thanks, i posted the full post. I just don't understand how the author solved the step, so i'm just wondering if anyone knows the formula because i want to write a program following his method of solving. I've written the EGCD to solve these, but i thought his approach was interesting enough to be worthy of me writing a program following his steps for the solution. So i'm trying to write his steps into a program but am stuck on how he derived the 3. – oppressionslayer Aug 09 '20 at 02:13
  • Sorry, I still don't follow. The first half of it seems to just define `h` to be the inverse of `327` (mod 1009). After that, I think it's picking `3` so that `3 * 327` is close to `1009` and tinkers with the numbers to get the end result. I don't see how that would generalize, though. – dxiv Aug 09 '20 at 02:21
  • I think he based his solution on a math paper or pdf math paper, i just need to find it, i really want to implement this method in a program as i've already written a lot of programs to find linear congruences and egcd's, but i thought this was a novel approach that i want to try writing a program for as i'm very interested in writing new programs for solving these types of problems. Modular Equation solving and different approaches are really interesting to me right now – oppressionslayer Aug 09 '20 at 02:23
  • I’m voting to close this question because it is about mathematics, not computer programming. Try the Math exchange. – user207421 Aug 09 '20 at 04:17
  • What programming language is this? It looks like Python – Joni Aug 09 '20 at 04:58
  • @Joni It's python – oppressionslayer Aug 09 '20 at 04:59
  • +1 to the answer and the edit. So in the end this looks like just a modified euclidean algorithm with signed remainders (`1009 = 3 * 327 + 28`, `327 = 12 * 28 - 9`, `28 = 3 * 9 - 1`), which could make it a bit easier to do by hand, indeed. Other than that, however, it's still a flavor of EGCD. – dxiv Aug 09 '20 at 05:29
  • Thanks dxiv! It does seem to be a flavor of EGCD, i'm just happy now that i can write a program to solve it this way to compare and learn since i'm really into this kind of math. – oppressionslayer Aug 09 '20 at 05:33

1 Answers1

2

What i don't understand is how the 3 is derived here, by what formula do you use to find the 3.

This is simply division with remainder.

Calculate 1009/327 and you get approximately 3.085626911. That tells you that 1009 = 3*327 + R, where the remainder R is less than 327.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • Joni, see my latest updates, your suggestion worked, and i wrote a verification method to ensure the results. I think you hit it on the nail, at least in my initial testing. – oppressionslayer Aug 09 '20 at 04:56