📜  Brahmagupta斐波那契身份

📅  最后修改于: 2021-04-30 02:13:03             🧑  作者: Mango

Brahmagupta Fibonacci身份指出,两个数字之积(每个数字是2平方和)可以用2种不同形式表示为2平方和。
数学上

一些示例是:

下面是一个程序,用于验证给定两个数字(两个平方之和)的Brahmagupta Fibonacci身份。

C++
// CPP code to verify
// Brahmagupta Fibonacci identity
#include 
using namespace std;
 
void find_sum_of_two_squares(int a,
                             int b)
{
    int ab = a*b;
 
    // represent the product
    // as sum of 2 squares
    for (int i = 0; i * i <= ab; i++)
    {
        for (int j = i; i * i +
                        j * j <= ab; j++)
        {
 
            // check identity criteria
            if (i * i + j * j == ab)
                cout << i << "^2 + " << j
                     << "^2 = " << ab << "\n";
        }
    }
}
 
// Driver code
int main()
{
    // 1^2 + 2^2
    int a = 1 * 1 + 2 * 2;
     
    // 3^2 + 4^2
    int b = 3 * 3 + 4 * 4;
 
    cout << "Representation of a * b as sum"
            " of 2 squares:\n";
 
    // express product of sum of 2 sqaures
    // as sum of (sum of 2 squares)
    find_sum_of_two_squares(a, b);
}


Java
// Java code to verify Brahmagupta
// Fibonacci identity
 
class GFG
{
    static void find_sum_of_two_squares(int a,
                                        int b)
{
    int ab = a * b;
 
    // represent the product
    // as sum of 2 squares
    for (int i = 0; i * i <= ab; i++)
    {
        for (int j = i; i * i +
                        j * j <= ab; j++)
        {
            // check identity criteria
            if (i * i + j * j == ab)
                System.out.println(i + "^2 + " +
                                   j +"^2 = " + ab);
        }
    }
}
 
// Driver code
public static void main(String[] args)
{
    // 1^2 + 2^2
    int a = 1 * 1 + 2 * 2;
     
    // 3^2 + 4^2
    int b = 3 * 3 + 4 * 4;
 
    System.out.println("Representation of a * b " +
                        "as sum of 2 squares:");
 
    // express product of sum
    // of 2 sqaures as sum of
    // (sum of 2 squares)
    find_sum_of_two_squares(a, b);
}
}
 
// This code is contributed
// by Smitha Dinesh Semwal


Python 3
# Python 3 code to verify
# Brahmagupta Fibonacci identity
 
def find_sum_of_two_squares(a, b):
 
    ab = a * b
 
    # represent the product
    # as sum of 2 squares
    i=0;
    while(i * i <= ab):
        j = i
        while(i * i + j * j <= ab):
 
            # check identity criteria
            if (i * i + j * j == ab):
                print(i,"^2 + ",j,"^2 = ",ab)
            j += 1
        i += 1
     
# Driver code
a = 1 * 1 + 2 * 2 # 1^2 + 2^2
b = 3 * 3 + 4 * 4 # 3^2 + 4^2
 
print("Representation of a * b as sum"
                     " of 2 squares:")
 
# express product of sum of 2 sqaures
# as sum of (sum of 2 squares)
find_sum_of_two_squares(a, b)
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# code to verify Brahmagupta
// Fibonacci identity
using System;
 
class GFG
{
    static void find_sum_of_two_squares(int a,
                                        int b)
    {
    int ab = a * b;
 
    // represent the product
    // as sum of 2 squares
    for (int i = 0; i * i <= ab; i++)
    {
        for (int j = i; i * i +
                        j * j <= ab; j++)
        {
            // check identity criteria
            if (i * i + j * j == ab)
                Console.Write(i + "^2 + " + j +
                          "^2 = " + ab + "\n");
        }
    }
}
 
// Driver code
public static void Main()
{
    // 1^2 + 2^2
    int a = 1 * 1 + 2 * 2;
     
    // 3^2 + 4^2
    int b = 3 * 3 + 4 * 4;
 
    Console.Write("Representation of a * b " +
                   "as sum of 2 squares:\n");
 
    // express product of sum of
    // 2 sqaures as sum of (sum of
    // 2 squares)
    find_sum_of_two_squares(a, b);
}
}
 
// This code is contributed
// by Smitha Dinesh Semwal


PHP


Javascript


输出 :
Representation of a * b as sum of 2 squares:
2^2 + 11^2 = 125
5^2 + 10^2 = 125