C++实现的大整数分解Pollard's rho算法程序

本文介绍了一种用于合数质因数分解的Pollard's Rho算法,并提供了一个使用C++实现的具体例子。该算法通过迭代计算来寻找非平凡因子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码来自GeeksforGeeksPollard’s Rho Algorithm for Prime Factorization

C++语言程序代码如下:

/* C++ program to find a prime factor of composite using
   Pollard's Rho algorithm */
#include<bits/stdc++.h>
using namespace std;
 
/* Function to calculate (base^exponent)%modulus */
long long int modular_pow(long long int base, int exponent,
                          long long int modulus)
{
    /* initialize result */
    long long int result = 1;
 
    while (exponent > 0)
    {
        /* if y is odd, multiply base with result */
        if (exponent & 1)
            result = (result * base) % modulus;
 
        /* exponent = exponent/2 */
        exponent = exponent >> 1;
 
        /* base = base * base */
        base = (base * base) % modulus;
    }
    return result;
}
 
/* method to return prime divisor for n */
long long int PollardRho(long long int n)
{
    /* initialize random seed */
    srand (time(NULL));
 
    /* no prime divisor for 1 */
    if (n==1) return n;
 
    /* even number means one of the divisors is 2 */
    if (n % 2 == 0) return 2;
 
    /* we will pick from the range [2, N) */
    long long int x = (rand()%(n-2))+2;
    long long int y = x;
 
    /* the constant in f(x).
     * Algorithm can be re-run with a different c
     * if it throws failure for a composite. */
    long long int c = (rand()%(n-1))+1;
 
    /* Initialize candidate divisor (or result) */
    long long int d = 1;  
 
    /* until the prime factor isn't obtained.
       If n is prime, return n */
    while (d==1)
    {
        /* Tortoise Move: x(i+1) = f(x(i)) */
        x = (modular_pow(x, 2, n) + c + n)%n;
 
        /* Hare Move: y(i+1) = f(f(y(i))) */
        y = (modular_pow(y, 2, n) + c + n)%n;
        y = (modular_pow(y, 2, n) + c + n)%n;
 
        /* check gcd of |x-y| and n */
        d = __gcd(abs(x-y), n);
 
        /* retry if the algorithm fails to find prime factor
         * with chosen x and c */
        if (d==n) return PollardRho(n);
    }
 
    return d;
}
 
/* driver function */
int main()
{
    long long int n = 10967535067;
    printf("One of the divisors for %lld is %lld.",
          n, PollardRho(n));
    return 0;
}


转载于:https://www.cnblogs.com/tigerisland/p/7564849.html

Pollard's rho algorithm是一种用于寻找大整数乘法下的隐含因子或离散对数的素数分解算法。在NTL库(Number Theoretic Library),这是一个广泛使用的C++库,它提供了高效的数学工具,包括处理大整数。 在NTL中实现Pollard rho算法求解离散对数可能会涉及以下几个步骤: 1. 定义函数生成器:通常选择两个互逆的函数f(x)和g(x),如f(x) = (x^2 + 1) % p 或者是一个随机多项式。 2. 初始化:选择两个初始值x0和y0,并计算它们的迭代值xi = f(xi) 和 yi = f(g(yi))。 3. 遍历过程:重复应用函数直到找到两个序列长度相近的质因数分解点,即存在整数t使得yi ≡ xi + t * gy模p。 4. 寻找线性关系:检查yi - xi 是否可以表示为两个连续项的差,即是否存在k使得yi - xi = k * (gi - gi-1) mod p。如果找到这样的线性关系,那么gcd(xi - gy, p) 可能就是离散对数的结果。 5. 如果找到了一个非平凡因子,则继续搜索直到找到p-1或者找到离散对数;否则,尝试调整初始值或函数生成器并继续。 在实际操作中,NTL会提供优化过的数据结构和函数来提高性能。以下是一个简化版的伪代码示例(注意,这只是一个概述,NTL的具体API和实现细节需要查看官方文档或源码): ```cpp #include <NTL/ZZX.h> // ... ZZX f(ZZX x); ZZX g(ZZX y, ZZ p); ZZX x0, y0; ZZ n; while (true) { x0 = f(x0); y0 = f(g(y0, p)); ZZX xi = x0, yi = y0; for (ZZ i = 1; i < n; ++i) { xi = f(xi); yi = f(g(yi, p)); if (yi - xi == k * (gi - g(i-1))) { // 找到线性关系,尝试解开离散对数 break; } } // ...处理其他情况和循环 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值