Znajdź unikalne pary takie, że każdy element jest mniejszy lub równy N
Mając liczbę całkowitą N, znajdź i pokaż liczbę par spełniających następujące warunki:
- Kwadrat odległości między tymi dwiema liczbami jest równy LCM z tych dwóch liczb.
- The GCD z tych dwóch liczb jest równa iloczynowi dwóch kolejnych liczb całkowitych.
- Obie liczby w parze powinny być mniejsze lub równe N.
NOTATKA: Powinny zostać wyświetlone tylko te pary, które spełniają jednocześnie oba powyższe warunki, a liczby te muszą być mniejsze lub równe N.
Przykłady:
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)
Wyjaśnienie:
Poniższe tabele dadzą jasny obraz tego, co można znaleźć:
Powyższe tabele przedstawiają GCD utworzone przez iloczyn dwóch kolejnych liczb i odpowiadających im wielokrotności, w których istnieje UNIKALNA PARA odpowiadająca każdej wartości. Zielone wpisy w każdym wierszu tworzą unikalną parę dla odpowiedniego GCD.
Notatka: W powyższych tabelach
- Dla pierwszego wpisu GCD=2 Pierwsza i druga wielokrotność liczby 2 tworzą Unikalną Parę (2 4)
- Podobnie dla 2. wpisu GCD=6 2. i 3. wielokrotność 6 tworzą Unikalną Parę (12 18)
- Podobnie przechodząc do wpisu Z-tego, tj. dla GCD = Z*(Z+1) jasne jest, że unikalna para będzie składać się z Z-tego i (Z+1)-tej wielokrotności GCD = Z*(Z+1). Teraz Z-ta wielokrotność NWD to Z * (Z*(Z+1)), a (Z+1)-ta wielokrotność NWD będzie wynosić (Z + 1) * (Z*(Z+1)).
- A ponieważ granicą jest N, druga liczba w unikalnej parze musi być mniejsza lub równa N. Zatem (Z + 1) * (Z*(Z+1)) <= N. Simplifying it further the desired relation is derived Z 3 + (2*Z 2 ) + Z <=N
Tworzy to wzór, a z obliczeń matematycznych wynika, że dla danego N całkowita liczba takich unikalnych par (powiedzmy Z) będzie zgodna z matematyczną zależnością pokazaną poniżej:
Z 3 + (2*Z 2 ) + Z <= N
Poniżej wymagana implementacja:
// 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 // 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 ) ') 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 < 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 ) + ')
' ); } } // Driver code let N = 500 pairs mul i = 1 ; pairs = No_Of_Pairs ( N ); document . write ( 'No. of pairs = ' + pairs + '
' ); print_pairs ( pairs ); // This code is contributed by mohit kumar 29 < /script>
C++14 // C++ code for the above approach: #include using namespace std ; // 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 ); cout < < 'Pair no. ' < < i < < ' --> (' < < ( mul * i ) < < ' ' < < mul * ( i + 1 ) < < ')' < < endl ;; } } // Driver Code int main () { int N = 500 pairs mul i = 1 ; pairs = No_Of_Pairs ( N ); cout < < 'No. of pairs = ' < < pairs < < endl ; print_pairs ( pairs ); return 0 ; }
Wyjście:
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)
Złożoność czasowa : NA 1/3 )
Przestrzeń pomocnicza : O(1)