Cal11 calculator

Write Sage Script to Calculate Number of Primitive Roots

Reviewed by Calculator Editorial Team

Primitive roots are essential in number theory and cryptography. This guide explains how to write a SageMath script to calculate the number of primitive roots modulo a prime number, including the formula, example, and calculator.

What Are Primitive Roots?

A primitive root modulo n is an integer g that generates all the integers modulo n through repeated exponentiation. In other words, g is a primitive root modulo n if the smallest positive integer k such that g^k ≡ 1 mod n is k = φ(n), where φ is Euler's totient function.

Primitive roots exist only for certain moduli. Specifically, for a prime p, there are exactly φ(p-1) primitive roots modulo p. For a prime power p^k, the number of primitive roots is φ(p^(k-1)(p-1)).

Sage Script to Calculate Primitive Roots

Below is a SageMath script that calculates the number of primitive roots modulo a given prime number:

def number_of_primitive_roots(p):
    """
    Calculate the number of primitive roots modulo a prime p.

    Input:
        p (integer): A prime number

    Output:
        Integer representing the number of primitive roots modulo p
    """
    if not p.is_prime():
        raise ValueError("Input must be a prime number")
    return euler_phi(p - 1)

The script uses Euler's totient function φ(n) to determine the number of primitive roots. For a prime p, φ(p-1) gives the exact count.

Example Calculation

Let's calculate the number of primitive roots modulo 7:

Step 1: Verify that 7 is prime (it is).

Step 2: Calculate φ(7-1) = φ(6).

Step 3: φ(6) = 2 (since 6 has two numbers less than it that are coprime: 1 and 5).

Therefore, there are 2 primitive roots modulo 7.

The primitive roots modulo 7 are 3 and 5.

Formula Used

The number of primitive roots modulo a prime p is given by Euler's totient function φ(p-1):

Number of primitive roots modulo p = φ(p-1)

Where φ(n) is the number of integers up to n that are coprime with n.

FAQ

What is the difference between primitive roots and generators?
Primitive roots and generators are essentially the same concept. Both refer to elements that generate the multiplicative group of integers modulo n.
Can I find primitive roots modulo a composite number?
Primitive roots exist only for certain moduli. For composite numbers, they exist only if the modulus is 2, 4, p^k, or 2p^k, where p is an odd prime.
How do I verify if a number is a primitive root?
To verify if g is a primitive root modulo n, check that the smallest positive integer k such that g^k ≡ 1 mod n is k = φ(n).