Her elemanın N'den küçük veya N'ye eşit olduğu benzersiz çiftleri bulun

Her elemanın N'den küçük veya N'ye eşit olduğu benzersiz çiftleri bulun

Bir N tamsayısı verildiğinde, aşağıdaki koşulları sağlayan çiftlerin sayısını bulun ve gösterin:

  • Bu iki sayı arasındaki mesafenin karesi eşittir LCM bu iki sayıdan
  • GCD Bu iki sayıdan biri ardışık iki tam sayının çarpımına eşittir.
  • Çiftteki her iki sayı da N'den küçük veya ona eşit olmalıdır.

NOT: Yalnızca yukarıdaki koşulların her ikisini de aynı anda karşılayan çiftler görüntülenmeli ve bu sayılar N'den küçük veya N'ye eşit olmalıdır.

Örnekler:   

  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) 

Açıklama:
Aşağıda gösterilen tablolar ne bulunacağına dair net bir fikir verecektir:  

Her elemanın N

Yukarıdaki tablolar, ardışık iki sayının ve her değere karşılık gelen BENZERSİZ ÇİFT'in mevcut olduğu katlarının çarpımından oluşan GCD'yi göstermektedir. Her satırdaki yeşil girişler, karşılık gelen GCD için benzersiz bir çift oluşturur.
Not: Yukarıdaki tablolarda  

  1. 1. giriş için GCD=2 2'nin 1. ve 2. katı Benzersiz Çifti oluşturur (2 4)
  2. Benzer şekilde 2. girdi olan GCD=6 için 6'nın 2. ve 3. katı Benzersiz Çifti oluşturur (12 18)
  3. Benzer şekilde Z'inci giriş için, yani GCD = Z*(Z+1) için devam edersek, benzersiz çiftin Zth ve GCD = Z*(Z+1)'in (Z+1)'inci katından oluşacağı açıktır. Şimdi OBEB'in Z'inci katı Z * (Z*(Z+1)) olur ve OBEB'in (Z+1)'inci katı (Z + 1) * (Z*(Z+1)) olacaktır.
  4. Ve limit N olduğundan, benzersiz çiftteki ikinci sayı N'den küçük veya ona eşit olmalıdır. Yani (Z + 1) * (Z*(Z+1)) <= N. Simplifying it further the desired relation is derived Z 3 + (2*Z 2 ) +Z <=N

Bu bir model oluşturur ve matematiksel hesaplamadan, belirli bir N için bu tür benzersiz çiftlerin (örneğin Z) toplam sayısının aşağıda gösterilen matematiksel ilişkiyi takip edeceği türetilir: 

Z 3  + (2*Z 2 ) + Z  <= N 


Gerekli uygulama aşağıdadır:  

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
      // 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  ;   }   

Çıkış:  
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) 

 

Zaman karmaşıklığı : AÇIK 1/3 )
Yardımcı alan : Ç(1)