HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →
Live examsInternational live exams are comingTell us which exam you want first. Timed practice papers are open now.See what is coming →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Space Science and Astronomy
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Python Labs
  2. /Every solution of a linear equation
MathematicsMediumHigh School (11–12)Number Theory

Every solution of a linear equation

You will write a solver for ax+by=cax + by = cax+by=c in non-negative integers, built on the extended Euclidean algorithm, and a tool for the coin problem.

The idea. The function extended_gcd(a, b) is given. It returns (g,s,t)(g, s, t)(g,s,t) with sa+tb=gsa + tb = gsa+tb=g. If g∤cg \nmid cg∤c there is no solution. Otherwise (scg,tcg)(s \tfrac{c}{g}, t \tfrac{c}{g})(sgc​,tgc​) is one solution, and the others step by bg\tfrac{b}{g}gb​ and ag\tfrac{a}{g}ga​.

Your tasks.

  1. Finish one_solution(a, b, c). Return a pair (x,y)(x, y)(x,y) with ax+by=cax + by = cax+by=c, or None when gcd⁡(a,b)∤c\gcd(a, b) \nmid cgcd(a,b)∤c.
  2. Finish non_negative(a, b, c). Return the sorted list of all pairs (x,y)(x, y)(x,y) with x,y≥0x, y \ge 0x,y≥0. A loop over xxx from 000 to c//ac // ac//a is enough.
  3. Finish largest_unpayable(a, b). Return the largest nnn from 111 to a×ba \times ba×b with non_negative(a, b, n) == [].

Run it first, just as it is. The checker at the end prints FAIL for each task. Then do the tasks one at a time, and run the program again after each one. A Hint appears under every check that still fails.

When every line says PASS, check the formula ab−a−bab - a - bab−a−b for several coprime pairs, such as (4, 9) and (7, 10).

StemPy LabContributed by StemPy Lab
python
Loading Python runtime…

A worked solution

Run your code once to unlock the worked solution.

Have you tried it?

Do the tasks in the editor first, and run the checker after each one. Its hints help when a check fails. When you are ready, compare your code with this solution.

Log in to save
← All Labs
← Orders, primitive roots and WilsonPythagorean triples by formula and by search →
More onNumber Theory

Discussion (0)

Log in to join the discussion.

Loading…

More labs on this topic

  1. Pythagorean triples by formula and by searchMedium
  2. Orders, primitive roots and WilsonMedium
  3. Euler's totient two waysMedium
  4. A solver for the Chinese remainder theoremMedium
  5. Fast powers modulo mMedium
All labs on this topic →