');\n\nvar as = [];\nvar bs = [];\nvar qs = [];\nvar q,r,old_r;\nwhile(r=a%b) {\n q=(a-r)/b;\n as.push(a);\n bs.push(b);\n qs.push(q);\n a=b;\n b=r;\n old_r=r;\n}\nvar s=0,t=1,tmp;\nwhile(qs.length) {\n q = qs.pop();\n a = as.pop();\n b = bs.pop();\n tmp = s;\n s=t;\n t=tmp - q*t;\n var expr = '('+s+')*('+a+') + ('+t+')*('+b+')';\n expr = Numbas.jme.display.exprToLaTeX(expr,['basic'],scope);\n list.append('- $'+old_r+' = '+expr+'$
');\n}\nreturn list;", "language": "javascript", "parameters": [["a", "number"], ["b", "number"]]}}, "parts": [{"gaps": [{"useCustomName": false, "type": "numberentry", "showFractionHint": true, "correctAnswerFraction": false, "mustBeReduced": false, "customName": "", "variableReplacements": [], "maxValue": "d", "correctAnswerStyle": "plain", "marks": 1, "scripts": {}, "adaptiveMarkingPenalty": 0, "minValue": "d", "notationStyles": ["plain", "en", "si-en"], "mustBeReducedPC": 0, "showCorrectAnswer": true, "showFeedbackIcon": true, "extendBaseMarkingAlgorithm": true, "variableReplacementStrategy": "originalfirst", "unitTests": [], "customMarkingAlgorithm": "", "allowFractions": false}, {"useCustomName": false, "type": "numberentry", "showFractionHint": true, "correctAnswerFraction": false, "mustBeReduced": false, "customName": "", "variableReplacements": [], "maxValue": "s", "correctAnswerStyle": "plain", "marks": 1, "scripts": {}, "adaptiveMarkingPenalty": 0, "minValue": "s", "notationStyles": ["plain", "en", "si-en"], "mustBeReducedPC": 0, "showCorrectAnswer": true, "showFeedbackIcon": true, "extendBaseMarkingAlgorithm": true, "variableReplacementStrategy": "originalfirst", "unitTests": [], "customMarkingAlgorithm": "", "allowFractions": false}, {"useCustomName": false, "type": "numberentry", "showFractionHint": true, "correctAnswerFraction": false, "mustBeReduced": false, "customName": "", "variableReplacements": [], "maxValue": "t", "correctAnswerStyle": "plain", "marks": 1, "scripts": {}, "adaptiveMarkingPenalty": 0, "minValue": "t", "notationStyles": ["plain", "en", "si-en"], "mustBeReducedPC": 0, "showCorrectAnswer": true, "showFeedbackIcon": true, "extendBaseMarkingAlgorithm": true, "variableReplacementStrategy": "originalfirst", "unitTests": [], "customMarkingAlgorithm": "", "allowFractions": false}], "useCustomName": false, "marks": 0, "scripts": {}, "adaptiveMarkingPenalty": 0, "type": "gapfill", "showFeedbackIcon": true, "extendBaseMarkingAlgorithm": true, "variableReplacementStrategy": "originalfirst", "unitTests": [], "showCorrectAnswer": true, "customName": "", "variableReplacements": [], "customMarkingAlgorithm": "", "sortAnswers": false, "prompt": "\n\n\nFind $d = \\operatorname{gcd}(\\var{a},\\var{b})$, the greatest common divisor of $\\var{a}$ and $\\var{b}$:
\n\n\n\n$d = \\phantom{{}}$[[0]]
\n\n\n\nNow find integers $s$ and $t$ such that:
\n\n\n\n\\[\\var{a}s+\\var{b}t = d\\]
\n\n\n\n$s = \\phantom{{}}$[[1]] $, t = \\phantom{{}}$[[2]]$.$
\n\n\n "}, {"gaps": [{"useCustomName": false, "type": "numberentry", "showFractionHint": true, "correctAnswerFraction": false, "mustBeReduced": false, "customName": "", "variableReplacements": [], "maxValue": "{least}", "correctAnswerStyle": "plain", "marks": 1, "scripts": {}, "adaptiveMarkingPenalty": 0, "minValue": "{least}", "notationStyles": ["plain", "en", "si-en"], "mustBeReducedPC": 0, "showCorrectAnswer": true, "showFeedbackIcon": true, "extendBaseMarkingAlgorithm": true, "variableReplacementStrategy": "originalfirst", "unitTests": [], "customMarkingAlgorithm": "", "allowFractions": false}], "useCustomName": false, "marks": 0, "scripts": {}, "adaptiveMarkingPenalty": 0, "type": "gapfill", "showFeedbackIcon": true, "extendBaseMarkingAlgorithm": true, "variableReplacementStrategy": "originalfirst", "unitTests": [], "showCorrectAnswer": true, "customName": "", "variableReplacements": [], "customMarkingAlgorithm": "", "sortAnswers": false, "prompt": "Find all solutions $x$ of the following congruence:
\n\\[\\var{a}x \\equiv \\var{n} \\mod \\var{b} \\]
\nThe least solution $x$ such that $0 \\lt x \\lt \\var{b}$ is: [[0]]
\n
"}], "variablesTest": {"maxRuns": 100, "condition": ""}, "tags": [], "metadata": {"description": "Given two numbers, find the gcd, then use Bézout's algorithm to find $s$ and $t$ such that $as+bt=\\operatorname{gcd}(a,b)$. Finally, find all solutions of an equation $\\mod b$.
", "licence": "Creative Commons Attribution 4.0 International"}, "variables": {"d": {"name": "d", "definition": "gcd(a,b)", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "s": {"name": "s", "definition": "extendedgcd1(max(a,b),min(a,b))", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "c": {"name": "c", "definition": "random(5..29)", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "bb": {"name": "bb", "definition": "randompow(3)*c", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "k": {"name": "k", "definition": "random(2..7)", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "least": {"name": "least", "definition": "k*s+diff*ceil(-k*s/diff)", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "describeleast": {"name": "describeleast", "definition": "if(s<0,'So, the first positive solution is \\\\[ x = \\\\simplify[]{{k*s} + {diff}*{ceil(-k*s/diff)} } = \\\\var{least}.\\\\]','')", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "a": {"name": "a", "definition": "max(a1,b1)", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "t": {"name": "t", "definition": "extendedgcd2(max(a,b),min(a,b))", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "n": {"name": "n", "definition": "k*d", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "a1": {"name": "a1", "definition": "randompow(3)*c", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "b1": {"name": "b1", "definition": "if(bb=a1,bb*randompow(2),bb)", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "b": {"name": "b", "definition": "min(a1,b1)", "description": "", "templateType": "anything", "group": "Ungrouped variables"}, "diff": {"name": "diff", "definition": "b/d", "description": "", "templateType": "anything", "group": "Ungrouped variables"}}, "rulesets": {}, "ungrouped_variables": ["s", "diff", "b1", "b", "n", "t", "a1", "bb", "c", "describeleast", "a", "least", "d", "k"], "extensions": [], "statement": "", "variable_groups": [], "preamble": {"css": "", "js": ""}, "advice": "a)
\nOn applying the standard method for finding the $\\operatorname{gcd}$ of two numbers we have the following sequence:
\n{describegcd(a,b)}
\nThe last non-zero remainder is $\\var{d}$, so this is the $\\operatorname{gcd}$ of $\\var{a}$ and $\\var{b}$.
\nNow work backwards through those steps, rearranging them to find the remainders as linear combinations of the other numbers.
\nWhen you reach the last line you will have found $s$ and $t$ such that
\\[\\var{a}s+\\var{b}t = \\var{d}\\]
\n{describebezout(a,b)}
\nSo $s = \\var{s}$ and $t = \\var{t}$.
\n
\nb)
\nFirst Step
\nWe would like to find all the solutions $x$ of the equation
\\[\\var{a}x \\equiv \\var{n} \\mod \\var{b}.\\]
\nIn order to solve such an equation first determine if there is a solution at all.
\n
\nLemma
\nAn equation of the form $ax \\equiv n \\mod b$ has a solution if and only if $\\operatorname{gcd}(a,b)|n$.
\n
\nSo the first task is to find $\\operatorname{gcd}(a,b)$ and see if it divides $n$. If not, there is no solution.
\nIn the first part, we found that $d = \\operatorname{gcd}(\\var{a},\\var{b}) = \\var{d}$.
\n$\\var{n} = \\var{d} \\times \\var{k}$, so the equation $\\var{a}x \\equiv \\var{n} \\mod \\var{b}$ has a solution.
\nFinding a solution
\nNote that because $d|n$, then $n=kd$ for some integer $k$.
\nIn order to find a solution of $x$ go back to the first part and use the integers $s$ and $t$ such that
\n\\[ \\var{a}s + \\var{b}t = d. \\]
\nIf we multiply both sides of this equation by $k$ we find that
\n\\[ \\var{a}ks + \\var{b}kt = kd = n, \\]
\nand by taking both sides modulo $\\var{b}$ we see that
\n\\[ \\var{a}ks \\equiv n \\mod \\var{b}. \\]
\nSo $x = ks = \\var{k*s}$ is a solution of the equation.
\nThe difference between successive terms is $\\frac{\\var{b}}{\\var{d}} = \\var{diff}$.
\n{describeLeast}
", "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}, {"name": "Blathnaid Sheridan", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/447/"}]}]}], "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}, {"name": "Blathnaid Sheridan", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/447/"}]}