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. /Fast powers modulo m
MathematicsMediumHigh School (11–12)Number Theory

Fast powers modulo m

You will write the method a computer uses to find the remainder of a huge power: repeated squaring. The checker compares your results with Python's own pow(a, k, m).

The idea. Write the exponent in binary. For example 13=8+4+113 = 8 + 4 + 113=8+4+1, so a13=a8⋅a4⋅aa^{13} = a^8 \cdot a^4 \cdot aa13=a8⋅a4⋅a. The powers a,a2,a4,a8a, a^2, a^4, a^8a,a2,a4,a8 come from squaring again and again, and you reduce modulo mmm after every step.

Your tasks.

  1. Finish power_mod(a, k, m). Keep a result and a base. While k > 0: if k is odd, multiply result by base; then square base and halve k with k // 2. Reduce modulo m every time.
  2. Finish digit_sum_mod9(n). Return the sum of the digits of n, reduced modulo 999.
  3. Finish last_two_digits(a, k). Return the last two digits of aka^kak as a number from 000 to 999999, using power_mod.

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, time a huge case: power_mod(3, 10 ** 18, 1000007). It needs only about sixty squarings.

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
← Bases and squaresA solver for the Chinese remainder theorem →
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. Every solution of a linear equationMedium
  3. Orders, primitive roots and WilsonMedium
  4. Euler's totient two waysMedium
  5. A solver for the Chinese remainder theoremMedium
All labs on this topic →