📜  大量的费马分解法

📅  最后修改于: 2021-04-27 05:13:18             🧑  作者: Mango

给定一个大数N ,任务是使用费马因数分解法将此数字划分为两个因子的乘积。

例子

Fermat因式分解: Fermat的因式分解方法基于奇数整数表示为两个平方之差。
对于整数N,我们想要a和b例如:

N = a2 - b2 = (a+b)(a-b) 

where (a+b) and (a-b) are 
the factors of the number N.

方法:

  1. 获取数字作为BigInteger类的对象
  2. 找出N的平方根。
  3. 确保a的值大于sqrt(N), b的值小于sqrt(N)。
  4. 将sqrt(n)的值作为a并递增该数字,直到并且除非找到数字b使得N – a ^ 2是一个完美的平方。

下面是上述方法的实现:

// Java program for Fermat's Factorization
// method for large numbers
  
import java.math.*;
import java.util.*;
  
class Solution {
  
    // Function to find the Floor
    // of square root of a number
    public static BigInteger sqrtF(BigInteger x)
        throws IllegalArgumentException
    {
        // if x is less than 0
        if (x.compareTo(BigInteger.ZERO) < 0) {
            throw new IllegalArgumentException(
                "Negative argument.");
        }
  
        // if x==0 or x==1
        if (x.equals(BigInteger.ZERO)
            || x.equals(BigInteger.ONE)) {
            return x;
        }
  
        BigInteger two
            = BigInteger.valueOf(2L);
        BigInteger y;
  
        // run a loop
        y = x.divide(two);
        while (y.compareTo(x.divide(y)) > 0)
            y = ((x.divide(y)).add(y))
                    .divide(two);
        return y;
    }
  
    // function to find the Ceil
    // of square root of a number
    public static BigInteger sqrtC(BigInteger x)
        throws IllegalArgumentException
    {
        BigInteger y = sqrtF(x);
  
        if (x.compareTo(y.multiply(y)) == 0) {
            return y;
        }
  
        else {
            return y.add(BigInteger.ONE);
        }
    }
  
    // Fermat factorisation
    static String FermatFactors(BigInteger n)
    {
        // constants
        BigInteger ONE = new BigInteger("1");
        BigInteger ZERO = new BigInteger("0");
        BigInteger TWO = new BigInteger("2");
  
        // if n%2 ==0 then return the factors
        if (n.mod(TWO).equals(ZERO)) {
            return n.divide(TWO)
                       .toString()
                + ", 2";
        }
  
        // find the square root
        BigInteger a = sqrtC(n);
  
        // if the number is a perfect square
        if (a.multiply(a).equals(n)) {
            return a.toString()
                + ", " + a.toString();
        }
  
        // else perform factorisation
        BigInteger b;
        while (true) {
            BigInteger b1 = a.multiply(a)
                                .subtract(n);
            b = sqrtF(b1);
  
            if (b.multiply(b).equals(b1))
                break;
            else
                a = a.add(ONE);
        }
  
        return a.subtract(b).toString()
            + ", " + a.add(b).toString();
    }
  
    // Driver code
    public static void main(String args[])
    {
        String N = "105327569";
  
        System.out.println(
            FermatFactors(
                new BigInteger(N)));
    }
}
输出:
10223, 10303

性能分析:

  • 时间复杂度: O(sqrt(N))
  • 空间复杂度: O(1)