给定一个整数N,找到并显示满足以下条件的对数:
null
注: 仅应显示同时符合上述两种条件的配对,且这些数字必须小于或等于N。
例如:
Input: 10Output: No. of pairs = 1 Pair no. 1 --> (2, 4)Input: 500Output: 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,其中每个值对应一对唯一。每行中的绿色条目构成对应GCD的唯一对。 注: 在上面的表格中,
- 对于第一个条目,GCD=2,2的第一个和第二个倍数构成唯一的一对(2,4)
- 同样,对于第二个条目,GCD=6,第二个和第三个6的倍数构成唯一的一对(12,18)
- 类似地,对于Zth条目,即对于GCD=Z*(Z+1),很明显,唯一对将由Zth和GCD=Z*(Z+1)的第(Z+1)个倍数组成。现在,GCD的Zth倍数是Z*(Z*(Z+1)),GCD的(Z+1)倍数将是(Z+1)*(Z*(Z+1))。
- 因为极限是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 <stdio.h> #include <stdlib.h> // 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)" , 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 " , 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
<?php // PHP program for finding // the required pairs // Finding the number // of unique pairs function No_Of_Pairs( $N ) { $i = 1; // Using the // derived formula while (( $i * $i * $i ) + (2 * $i * $i ) + $i <= $N ) $i ++; return ( $i - 1); } // Printing the unique pairs function print_pairs( $pairs ) { $i = 1; $mul ; for ( $i = 1; $i <= $pairs ; $i ++) { $mul = $i * ( $i + 1); echo "Pair no." , $i , " --> (" , ( $mul * $i ), ", " , $mul * ( $i + 1), ") " ; } } // Driver Code $N = 500; $pairs ; $mul ; $i = 1; $pairs = No_Of_Pairs( $N ); echo "No. of pairs = " , $pairs , " " ; print_pairs( $pairs ); // This code is contributed // by Akanksha Rai(Abby_akku) ?> |
Javascript
<script> // Javascript program for finding the // required pairs // Finding the number of unique pairs function No_Of_Pairs(N) { let i = 1; // Using the derived formula while ((i * i * i) + (2 * i * i) + i <= N) i++; return (i - 1); } // Printing the unique pairs function print_pairs(pairs) { let i = 1, mul; for (i = 1; i <= pairs; i++) { mul = i * (i + 1); document.write( "Pair no. " + i + " --> (" + (mul * i) + ", " + mul * (i + 1) + ")<br>" ); } } // Driver code let N = 500, pairs, mul, i = 1; pairs = No_Of_Pairs(N); document.write( "No. of pairs = " + pairs + "<br>" ); print_pairs(pairs); // This code is contributed by mohit kumar 29 </script> |
输出:
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)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END