Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- #
- # ax ≡ b (mod c)
- # Looking for two smallest positive numbers (x) satisfying equation ax - b ≡ 0 (mod c)
- # Having two solutions m & n is possible to determine formula for all solutions
- #
- # n - m is a difference between next solutions; m is smaller then n;
- # Hence if ax = b (mod c), x = (n - m)t + m
- #
- def getEquation(a, b, c):
- if a is c and b is not 0 and b is not c:
- return "There is no solution"
- congruent = u"\u2261".encode('utf-8')
- answer = "{0}x {1} {2} (mod {3}) \n".format(a, congruent, b, c) # ax ≡ b (mod c)
- if a %c is not a or b % c is not b:
- answer += "{0}x {1} {2} (mod {3}) \n".format(a % c, congruent, b % c, c)
- solutions, i = [], 0
- a %= c
- b %= c
- while len(solutions) is not 2:
- if i > 2 * c: return "There is no solution"
- if (a * i - b) % c is 0: solutions.append(i)
- i += 1
- answer += "x {0} {1} (mod {2}) \n".format(congruent, solutions[0], solutions[1] - solutions[0]) # x ≡ m (mod n - m)
- answer += "x = {0}t + {1}".format(solutions[1] - solutions[0], solutions[0]) # x = (n - m)*t + m
- return answer
- print(getEquation(411, 3207, 911))
Advertisement
Add Comment
Please, Sign In to add comment