代码之家  ›  专栏  ›  技术社区  ›  oppressionslayer

如何更改代码以使用除法解模方程

  •  1
  • oppressionslayer  · 技术社区  · 5 年前

    (743360//1008//x)=272 (mod 1009)

    哪个 Modular Equation Solver

    743360/1008x = 272 (mod 1009)

    x == 116

    这是我的密码:

    def fastlinearcongruenceSO(powx, divmodx, N, withstats=False): 
     x, y, z = egcditerx2SO(powx, N, withstats) 
     answer = (y*divmodx)%N 
     if withstats == True: 
        print(f"answer = {answer}") 
     if x > 1: 
        powx//=x 
        divmodx//=x 
        N//=x 
        if withstats == True: 
          print(f"powx = {powx}, divmodx = {divmodx}, N = {N}") 
        x, y, z = egcditerx2SO(powx, N, withstats) 
        if withstats == True: 
          print(f"x = {x}, y = {y}, z = {z}") 
        answer = (y*divmodx)%N 
        if withstats == True: 
           print(f"answer = {answer}") 
     return answer
    
    def egcditerx2SO(a, b, withstats=False): 
      s = 0 
      r = b 
      old_s = 1 
      old_r = a 
      quotient = 0
      if withstats == True: 
          print(f"quotient = {quotient}, old_r = {old_r}, r = {r}, old_s = {old_s}, s = {s}") 
      while r!= 0: 
        quotient = old_r // r 
        old_r, r = r, old_r - quotient * r 
        old_s, s = s, old_s - quotient * s 
        if withstats == True: 
          print(f"quotient = {quotient}, old_r = {old_r}, r = {r}, old_s = {old_s}, s = {s}") 
      if b != 0: 
        bezout_t = quotient = (old_r - old_s * a) // b 
        if withstats == True: 
          print(f"bezout_t = {bezout_t}") 
      else: 
        bezout_t = 0 
      if withstats == True: 
        print("Bézout coefficients:", (old_s, bezout_t)) 
        print("greatest common divisor:", old_r) 
      return old_r, old_s, bezout_t
    

    IN: fastlinearcongruenceSO(327, 1, 1009)
    OUT: 108
    

    我不知道我需要做什么或怎样的修改才能用除法的形式来解决它,有人知道我需要做什么修改,或者我是否可以用我现有的代码来解决它吗?我真的想修改我的代码来处理它解决这个方程: 743360/1008x = 272 (mod 1009) x == 116

    enter image description here

    0 回复  |  直到 5 年前