📌  相关文章
📜  查找唯一的对,以使每个元素小于或等于N

📅  最后修改于: 2021-04-29 14:49:35             🧑  作者: Mango

给定整数N,找到并显示满足以下条件的对数:

  • 这两个数字之间的距离的平方等于这两个数字的LCM。
  • 这两个数字的GCD等于两个连续整数的乘积。
  • 该对中的两个数字均应小于或等于N。

注意:仅应显示同时符合上述两个条件的那些对,并且这些数字必须小于或等于N。

例子:

Input: 10
Output: No. of pairs = 1
        Pair no. 1 --> (2, 4)

Input: 500
Output: No. of pairs = 7
        Pair no. 1 --> (2, 4)
        Pair no. 2 --> (12, 18)
        Pair no. 3 --> (36, 48)
        Pair no. 4 --> (80, 100)
        Pair no. 5 --> (150, 180)
        Pair no. 6 --> (252, 294)
        Pair no. 7 --> (392, 448)

解释:
下表显示了可以找到的清晰视图:

上表显示了由两个连续数字与其对应的倍数乘积形成的GCD,其中UNIQUE PAIR存在于每个值。每行中的绿色条目形成对应GCD的唯一对。
注意:在上表中,

  1. 对于第1个条目,GCD = 2、2的1st和2nd的倍数构成唯一对(2,4)
  2. 同样,对于第二项,GCD = 6,第二和第六的三倍构成唯一对(12,18)
  3. 类似地,继续前进,对于第Z个条目,即对于GCD = Z *(Z + 1),很明显唯一对将包含GCD = Z *(Z + 1)的Zth和第(Z + 1)倍。 。现在,GCD的第Z倍为Z *(Z *(Z + 1)),GCD的第(Z + 1)倍为(Z +1)*(Z *(Z + 1))。
  4. 并且由于限制为N,因此唯一对中的第二个数字必须小于或等于N。因此,(Z +1)*(Z *(Z + 1))<=N。期望的关系为Z 3 +(2 * Z 2 )+ Z <= N

这形成一个模式,并从数学计算中得出,对于给定的N,此类唯一对(例如Z)的总数将遵循以下所示的数学关系:

Z3 + (2*Z2) + Z <= N

以下是所需的实现:

C
// C program for finding the required pairs
#include 
#include 
 
// Finding the number of unique pairs
int No_Of_Pairs(int N)
{
    int i = 1;
 
    // Using the derived formula
    while ((i * i * i) + (2 * i * i) + i <= N)
        i++;
 
    return (i - 1);
}
 
// Printing the unique pairs
void print_pairs(int pairs)
{
    int i = 1, mul;
    for (i = 1; i <= pairs; i++) {
        mul = i * (i + 1);
        printf("Pair no. %d --> (%d, %d)\n",
               i, (mul * i), mul * (i + 1));
    }
}
 
// Driver program to test above functions
int main()
{
    int N = 500, pairs, mul, i = 1;
    pairs = No_Of_Pairs(N);
 
    printf("No. of pairs = %d \n", pairs);
    print_pairs(pairs);
 
    return 0;
}


Java
// Java program for finding
// the required pairs
import java.io.*;
 
class GFG
{
     
    // Finding the number
    // of unique pairs
    static int No_Of_Pairs(int N)
    {
        int i = 1;
     
        // Using the derived formula
        while ((i * i * i) +
               (2 * i * i) + i <= N)
            i++;
     
        return (i - 1);
    }
     
    // Printing the unique pairs
    static void print_pairs(int pairs)
    {
        int i = 1, mul;
        for (i = 1; i <= pairs; i++)
        {
            mul = i * (i + 1);
            System.out.println("Pair no. " + i + " --> (" +
                                         (mul * i) + ", " +
                                      mul * (i + 1) + ")");
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int N = 500, pairs, mul, i = 1;
        pairs = No_Of_Pairs(N);
     
        System.out.println("No. of pairs = " + pairs);
        print_pairs(pairs);
    }
}
 
// This code is contributed by Mahadev.


Python3
# Python3 program for finding the required pairs
 
# Finding the number of unique pairs
def No_Of_Pairs(N):
 
    i = 1;
 
    # Using the derived formula
    while ((i * i * i) + (2 * i * i) + i <= N):
        i += 1;
 
    return (i - 1);
 
# Printing the unique pairs
def print_pairs(pairs):
 
    i = 1;
    mul = 0;
    for i in range(1, pairs + 1):
        mul = i * (i + 1);
        print("Pair no." , i, " --> (", (mul * i),
                        ", ", mul * (i + 1), ")");
 
# Driver Code
N = 500;
i = 1;
pairs = No_Of_Pairs(N);
 
print("No. of pairs = ", pairs);
print_pairs(pairs);
 
# This code is contributed
# by mits


C#
// C# program for finding
// the required pairs
using System;
 
class GFG
{
     
// Finding the number
// of unique pairs
static int No_Of_Pairs(int N)
{
    int i = 1;
 
    // Using the derived formula
    while ((i * i * i) +
           (2 * i * i) + i <= N)
        i++;
 
    return (i - 1);
}
 
// Printing the unique pairs
static void print_pairs(int pairs)
{
    int i = 1, mul;
    for (i = 1; i <= pairs; i++)
    {
        mul = i * (i + 1);
        Console.WriteLine("Pair no. " + i + " --> (" +
                                    (mul * i) + ", " +
                                 mul * (i + 1) + ")");
    }
}
 
// Driver code
static void Main()
{
    int N = 500, pairs;
    pairs = No_Of_Pairs(N);
 
    Console.WriteLine("No. of pairs = " +
                                  pairs);
    print_pairs(pairs);
}
}
 
// This code is contributed by mits


PHP
 (" ,
             ($mul * $i), ", ",
              $mul * ($i + 1),") \n";
    }
}
 
// Driver Code
$N = 500; $pairs;
$mul; $i = 1;
$pairs = No_Of_Pairs($N);
 
echo "No. of pairs = ",
        $pairs , " \n";
print_pairs($pairs);
 
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>


Javascript


输出:
No. of pairs = 7 
Pair no. 1 --> (2, 4)
Pair no. 2 --> (12, 18)
Pair no. 3 --> (36, 48)
Pair no. 4 --> (80, 100)
Pair no. 5 --> (150, 180)
Pair no. 6 --> (252, 294)
Pair no. 7 --> (392, 448)