Poiščite edinstvene pare, tako da je vsak element manjši ali enak N

Poiščite edinstvene pare, tako da je vsak element manjši ali enak N

Za dano celo število N poiščite in pokažite število parov, ki izpolnjujejo naslednje pogoje:

  • Kvadrat razdalje med tema dvema številoma je enak LCM teh dveh številk.
  • The GCD teh dveh števil je enako zmnožku dveh zaporednih celih števil.
  • Obe števili v paru morata biti manjši ali enaki N.

OPOMBA: Prikazani morajo biti le tisti pari, ki hkrati izpolnjujejo oba zgornja pogoja, te številke pa morajo biti manjše ali enake N.

Primeri:   



  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) 

Pojasnilo:
Spodnje tabele bodo dale jasen vpogled v to, kaj je treba najti:  

Poiščite edinstvene pare, tako da je vsak element manjši ali enak N

Zgornje tabele prikazujejo GCD, ki ga tvori zmnožek dveh zaporednih števil in pripadajočih večkratnikov, v katerih obstaja UNIQUE PAIR, ki ustreza vsaki vrednosti. Zeleni vnosi v vsaki vrstici tvorijo edinstven par za ustrezen GCD.
Opomba: V zgornjih tabelah  

  1. Za 1. vnos GCD=2 1. in 2. večkratnik 2 tvorita edinstveni par (2 4)
  2. Podobno za 2. vnos GCD=6 2. in 3. večkratnik števila 6 tvorita edinstveni par (12 18)
  3. Podobno gremo naprej za Zth vnos, tj. za GCD = Z*(Z+1), je jasno, da bo edinstven par sestavljal Zth in (Z+1)-ti večkratnik GCD = Z*(Z+1). Zdaj je Z-kratnik GCD Z * (Z*(Z+1)) in (Z+1)-ti večkratnik GCD bo (Z + 1) * (Z*(Z+1)).
  4. In ker je meja N, mora biti drugo število v edinstvenem paru manjše ali enako N. Torej (Z + 1) * (Z*(Z+1)) <= N. Simplifying it further the desired relation is derived Z 3 + (2*Z 2 ) + Z <=N

To tvori vzorec in iz matematičnega izračuna je izpeljano, da bo za dano N skupno število takih edinstvenih parov (recimo Z) sledilo matematični zvezi, prikazani spodaj: 

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


Spodaj je zahtevana izvedba:  

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

Izhod:  
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) 

 

Časovna zapletenost : O(N 1/3 )
Pomožni prostor : O(1)


Top Članki

Kategorija

Zanimivi Članki