📜  费马分解法

📅  最后修改于: 2021-04-21 22:17:33             🧑  作者: Mango

Fermat的因式分解方法基于奇数整数表示为两个平方之差。
对于整数n ,我们需要ab,例如:

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

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

例子:

Input: n = 6557
Output: [79,83]
Explanation: 
For the above value, 
the first try for a is ceil value 
of square root of 6557, which is 81.

Then, 
b2 = 812 - 6557 = 4,
as it is a perfect square.
So, b = 2

So, the factors of 6557 are:  
(a - b) = 81 -2  = 79 & 
(a + b) = 81 + 2 = 83.

方法 :

  1. 如果n = pq是将n分解为两个正整数,则由于n为奇数,因此p和q均为奇数。
  2. 设a = 1/2 *(p + q)和b = 1/2 *(qp)。
  3. 由于a和b都是整数,则p =(a – b)和q =(a + b)。
  4. 因此, n = pq =(a – b)(a + b)= a 2 – b 2
  5. 在质数的情况下,我们返回直到b = 1,因为质数的一个因数是1。
  6. while循环可确保此操作

下面是上述方法的实现

C++
// C++ implementation of fermat's factorization 
#include
  
using namespace std;
  
    // This function finds the value of a and b 
    // and returns a+b and a-b 
    void FermatFactors(int n) 
    { 
          
        // since fermat's factorization applicable 
        // for odd positive integers only 
        if(n <= 0) 
        { 
            cout << "[" << n << "]"; 
            return; 
        } 
      
        // check if n is a even number 
        if((n & 1) == 0) 
        { 
            cout << "[" << n / 2.0 << "," << 2 << "]"; 
            return; 
        } 
              
        int a = ceil(sqrt(n)) ; 
      
        // if n is a perfect root, 
        // then both its square roots are its factors 
        if(a * a == n) 
        { 
            cout << "[" << a << "," << a << "]"; 
            return; 
        } 
        int b; 
        while(true) 
        { 
            int b1 = a * a - n ; 
            b = (int)sqrt(b1) ; 
              
            if(b * b == b1) 
                break; 
            else
                a += 1; 
        } 
        cout << "[" << (a - b) << "," << (a + b) << "]" ; 
        return; 
    } 
          
    // Driver Code 
    int main() 
    {
        FermatFactors(6557);
        return 0;
    }
  
// This code is contributed by AnkitRai01


Java
// Java implementation of fermat's factorization 
class GFG 
{
      
    // This function finds the value of a and b 
    // and returns a+b and a-b 
    static void FermatFactors(int n)
    {
          
        // since fermat's factorization applicable 
        // for odd positive integers only 
        if(n <= 0)
        {
            System.out.print("["+ n + "]");
            return;
        }
      
        // check if n is a even number 
        if((n & 1) == 0)
        {
            System.out.print("[" + n / 2.0 + "," + 2 + "]"); 
            return;
        }
              
        int a = (int)Math.ceil(Math.sqrt(n)) ;
      
        // if n is a perfect root, 
        // then both its square roots are its factors 
        if(a * a == n)
        {
            System.out.print("[" + a + "," + a + "]"); 
            return;
        }
        int b;
        while(true)
        {
            int b1 = a * a - n ;
            b = (int)(Math.sqrt(b1)) ;
              
            if(b * b == b1)
                break;
            else
                a += 1;
        }
        System.out.print("[" + (a - b) +"," + (a + b) + "]" ); 
        return;
    }
          
    // Driver Code 
    public static void main (String[] args) 
    {
        FermatFactors(6557);
    }
}
  
// This code is contributed by AnkitRai01


Python3
# Python 3 implementation of fermat's factorization
  
from math import ceil, sqrt
  
#This function finds the value of a and b
#and  returns a+b and a-b
def FermatFactors(n):
  
   # since fermat's factorization applicable 
   # for odd positive integers only
    if(n<= 0):
        return [n]  
  
    # check if n is a even number 
    if(n & 1) == 0:  
        return [n / 2, 2] 
          
    a = ceil(sqrt(n))
  
    #if n is a perfect root, 
    #then both its square roots are its factors
    if(a * a == n):
        return [a, a]
  
    while(True):
        b1 = a * a - n 
        b = int(sqrt(b1))
        if(b * b == b1):
            break
        else:
            a += 1 
    return [a-b, a + b]
      
# Driver Code 
print(FermatFactors(6557))


C#
// C# implementation of fermat's factorization 
using System;
  
class GFG 
{ 
      
    // This function finds the value of a and b 
    // and returns a+b and a-b 
    static void FermatFactors(int n) 
    { 
          
        // since fermat's factorization applicable 
        // for odd positive integers only 
        if(n <= 0) 
        { 
            Console.Write("["+ n + "]"); 
            return; 
        } 
      
        // check if n is a even number 
        if((n & 1) == 0) 
        { 
            Console.Write("[" + n / 2.0 + "," + 2 + "]"); 
            return; 
        } 
              
        int a = (int)Math.Ceiling(Math.Sqrt(n)) ; 
      
        // if n is a perfect root, 
        // then both its square roots are its factors 
        if(a * a == n) 
        { 
            Console.Write("[" + a + "," + a + "]"); 
            return; 
        } 
        int b; 
        while(true) 
        { 
            int b1 = a * a - n ; 
            b = (int)(Math.Sqrt(b1)) ; 
              
            if(b * b == b1) 
                break; 
            else
                a += 1; 
        } 
        Console.Write("[" + (a - b) +"," + (a + b) + "]" ); 
        return; 
    } 
          
    // Driver Code 
    public static void Main () 
    { 
        FermatFactors(6557); 
    } 
} 
  
// This code is contributed by AnkitRai01


输出:
[79, 83]