Primitive Roots(原根个数)

本文介绍了一种计算模素数p下原根数量的方法,并给出了一段C++代码实现。利用欧拉函数φ(n),可以快速计算出特定范围内所有奇素数p的原根个数。

Primitive Roots

We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if the set { (x i mod p) | 1 <= i <= p-1 } is equal to { 1, ..., p-1 }. For example, the consecutive powers of 3 modulo 7 are 3, 2, 6, 4, 5, 1, and thus 3 is a primitive root modulo 7.
Write a program which given any odd prime 3 <= p < 65536 outputs the number of primitive roots modulo p.

Input
Each line of the input contains an odd prime numbers p. Input is terminated by the end-of-file seperator.
Output
For each p, print a single number that gives the number of primitive roots in a single line.
Sample Input

23
31
79

Sample Output

10
8
24
题意:

求模素数p的原根个数

分析:

结论:如果p是素数,则模p必有原根,其个数为ϕ(p1)ϕ(p−1)

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5+10;
int phi[maxn];
void Euler(){
    for(int i = 1; i < maxn; i++){
        phi[i] = i;
    }
    for(int i = 2; i < maxn; i++){
        if(phi[i] == i){
            for(int j = i; j < maxn; j += i){
                phi[j] = phi[j] / i * (i - 1);
            }
        }
    }
}
int main(){
    Euler();
    int p;
    while(~scanf("%d",&p)){
        printf("%d\n",phi[p-1]);
    }
    return 0;
}
### 模47的所有原根 在数论中,模 $ n $ 的原根是指一个整数 $ g $,使得其模 $ n $ 的幂能够生成所有与 $ n $ 互素的整数。对于质数 $ p $,其模 $ p $ 的原根的数量为 $ \phi(p-1) $,其中 $ \phi $ 是欧拉函数[^1]。 对于模 47 的情况,47 是一个质数,因此需要找到所有满足以下条件的整数 $ g $: $$ g^k \mod 47 $$ 能够生成所有从 1 到 46 的整数(即模 47 下的乘法群的所有元素)。这意味着 $ g $ 的阶必须等于 $ \phi(47) = 46 $。 #### 计算过程 1. 首先计算 $ \phi(46) $,因为 $ 46 = 2 \times 23 $,所以: $$ \phi(46) = 46 \times (1 - \frac{1}{2}) \times (1 - \frac{1}{23}) = 22 $$ 因此,模 47 的原根数量为 22。 2. 接下来验证哪些整数是模 47 的原根。一个整数 $ g $ 是模 47 的原根,当且仅当对于所有 $ d $ 满足 $ d \mid 46 $ 且 $ d < 46 $,都有: $$ g^d \not\equiv 1 \pmod{47} $$ 3. 模 47 的所有因子为 $ 1, 2, 23, 46 $。因此,只需验证 $ g^{2} \mod 47 $ 和 $ g^{23} \mod 47 $ 是否都不等于 1。 #### 示例代码 以下是一个 Python 程序,用于计算模 47 的所有原根: ```python def is_primitive_root(g, p): if gcd(g, p) != 1: return False factors = [2, 23] # Factors of p-1 = 46 for factor in factors: if pow(g, (p-1) // factor, p) == 1: return False return True def gcd(a, b): while b: a, b = b, a % b return a p = 47 primitive_roots = [g for g in range(1, p) if is_primitive_root(g, p)] print(primitive_roots) ``` 运行上述代码后,可以得到模 47 的所有原根为: $$ \{5, 10, 11, 13, 15, 19, 20, 22, 23, 26, 28, 29, 30, 31, 33, 35, 38, 39, 40, 41, 42, 44\} $$ 这些整数构成了模 47 的所有原根集合。 ### 原根的应用 原根在密码学和加密算法中有重要应用,例如 Diffie-Hellman 密钥交换协议和 RSA 加密算法中都涉及原根的概念。此外,在快速数论变换(NTT)中,原根也被用来替代复数单位根,从而实现高效的模运算[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值