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. /Orders, primitive roots and Wilson
MathematicsMediumHigh School (11–12)Number Theory

Orders, primitive roots and Wilson

You will write three small tools: the order of a number, a primitive-root finder and Wilson's test for primes.

The idea. The order of aaa modulo nnn is the least k≥1k \ge 1k≥1 with ak≡1a^k \equiv 1ak≡1. A primitive root modulo a prime ppp is a number of order p−1p - 1p−1. Wilson's test says n≥2n \ge 2n≥2 is prime exactly when (n−1)!≡−1(modn)(n - 1)! \equiv -1 \pmod n(n−1)!≡−1(modn).

Your tasks.

  1. Finish order(a, n). Multiply by aaa step by step until the remainder is 111, and count the steps.
  2. Finish smallest_primitive_root(p). Return the least ggg from 222 upwards with order(g, p) == p - 1.
  3. Finish wilson_prime(n). Build (n−1)!(n - 1)!(n−1)! modulo nnn in a loop, reducing as you go, and test it against n−1n - 1n−1.

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, print the smallest primitive root of each prime below 505050. Which value appears most often?

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
← Euler's totient two waysEvery solution of a linear equation →
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. Euler's totient two waysMedium
  4. A solver for the Chinese remainder theoremMedium
  5. Fast powers modulo mMedium
All labs on this topic →