Finden Sie die Summe der Teiler aller Teiler einer natürlichen Zahl

Finden Sie die Summe der Teiler aller Teiler einer natürlichen Zahl
Probieren Sie es bei GfG Practice aus #practiceLinkDiv { display: none !important; }

Gegeben sei eine natürliche Zahl N Die Aufgabe besteht darin, die Summe der Teiler aller Teiler von n zu ermitteln.

Beispiele:  

  Input :   n = 54   Output :   232 Divisors of 54 = 1 2 3 6 9 18 27 54. Sum of divisors of 1 2 3 6 9 18 27 54 are 1 3 4 12 13 39 40 120 respectively. Sum of divisors of all the divisors of 54 = 1 + 3 + 4 + 12 + 13 + 39 + 40 + 120 = 232.   Input :   n = 10   Output :   28 Divisors of 10 are 1 2 5 10 Sums of divisors of divisors are 1 3 6 18. Overall sum = 1 + 3 + 6 + 18 = 28 
Recommended Practice Finden Sie die Summe der Teiler Probieren Sie es aus!

Mit der Tatsache, dass eine beliebige Zahl N kann als Produkt von Primfaktoren ausgedrückt werden N = S 1 k1 x p 2 k2 x ... wo p 1 P 2 ... sind Primzahlen. 
Alle Teiler von n können als p ausgedrückt werden 1 A x p 2 B x ... wobei 0 <= a <= k1 and 0 <= b <= k2. 
Jetzt ist die Summe der Teiler die Summe aller Potenzen von p 1 - P 1 P 1 1 .... P 1 k1 multipliziert mit der ganzen Potenz von p 2 - P 2 P 2 1 .... P 2 k1  
Summe des Teilers von n 
= (S 1 x p 2 ) + (S 1 1 x p 2 ) +.....+ (S 1 k1 x p 2 ) +....+ (S 1 x p 2 1 ) + (S 1 1 x p 2 1 ) +.....+ (S 1 k1 x p 2 1 ) +.........+ 
   (P 1 x p 2 k2 ) + (S 1 1 x p 2 k2 ) +......+ (S 1 k1 x p 2 k2 ). 
= (S 1 + S 1 1 +...+ S 1 k1 ) x p 2 + (S 1 + S 1 1 +...+ S 1 k1 ) x p 2 1 +.......+ (S 1 + S 1 1 +...+ S 1 k1 ) x p 2 k2
= (S 1 + S 1 1 +...+ S 1 k1 ) x (S 2 + S 2 1 +...+ S 2 k2 ).



Nun sind die Teiler eines beliebigen p A für p als Primzahl sind p P 1 ...... P A . Und die Summe der Teiler beträgt (S (a+1) - 1)/(p -1) sei durch f(p) definiert. 
Die Summe der Teiler aller Teiler ergibt sich also 
= (f(S 1 ) + f(S 1 1 ) +...+ f(S 1 k1 )) x (f(S 2 ) + f(S 2 1 ) +...+ f(S 2 k2 )).

Wenn wir also eine Zahl n durch Primfaktorzerlegung gegeben haben, können wir die Summe der Teiler aller Teiler ermitteln. Aber in diesem Problem wird uns gegeben, dass n das Produkt des Elements des Arrays ist. Finden Sie also die Primfaktorzerlegung jedes Elements und nutzen Sie die Tatsache a B x a C = a b+c .

Nachfolgend finden Sie die Umsetzung dieses Ansatzes:  

C++
   // C++ program to find sum of divisors of all   // the divisors of a natural number.   #include       using     namespace     std  ;   // Returns sum of divisors of all the divisors   // of n   int     sumDivisorsOfDivisors  (  int     n  )   {      // Calculating powers of prime factors and      // storing them in a map mp[].      map   <  int       int  >     mp  ;      for     (  int     j  =  2  ;     j   <=  sqrt  (  n  );     j  ++  )      {      int     count     =     0  ;      while     (  n  %  j     ==     0  )      {      n     /=     j  ;      count  ++  ;      }      if     (  count  )      mp  [  j  ]     =     count  ;      }      // If n is a prime number      if     (  n     !=     1  )      mp  [  n  ]     =     1  ;      // For each prime factor calculating (p^(a+1)-1)/(p-1)      // and adding it to answer.      int     ans     =     1  ;      for     (  auto     it     :     mp  )      {      int     pw     =     1  ;      int     sum     =     0  ;      for     (  int     i  =  it  .  second  +  1  ;     i  >=  1  ;     i  --  )      {      sum     +=     (  i  *  pw  );      pw     *=     it  .  first  ;      }      ans     *=     sum  ;      }      return     ans  ;   }   // Driven Program   int     main  ()   {      int     n     =     10  ;      cout      < <     sumDivisorsOfDivisors  (  n  );      return     0  ;   }   
Java
   // Java program to find sum of divisors of all    // the divisors of a natural number.    import     java.util.HashMap  ;   class   GFG      {      // Returns sum of divisors of all the divisors      // of n      public     static     int     sumDivisorsOfDivisors  (  int     n  )      {      // Calculating powers of prime factors and      // storing them in a map mp[].      HashMap   <  Integer       Integer  >     mp     =     new     HashMap   <>  ();      for     (  int     j     =     2  ;     j      <=     Math  .  sqrt  (  n  );     j  ++  )         {      int     count     =     0  ;      while     (  n     %     j     ==     0  )         {      n     /=     j  ;      count  ++  ;      }      if     (  count     !=     0  )      mp  .  put  (  j       count  );      }      // If n is a prime number      if     (  n     !=     1  )      mp  .  put  (  n       1  );      // For each prime factor calculating (p^(a+1)-1)/(p-1)      // and adding it to answer.      int     ans     =     1  ;      for     (  HashMap  .  Entry   <  Integer       Integer  >     entry     :     mp  .  entrySet  ())         {      int     pw     =     1  ;      int     sum     =     0  ;      for     (  int     i     =     entry  .  getValue  ()     +     1  ;     i     >=     1  ;     i  --  )      {      sum     +=     (  i     *     pw  );      pw     *=     entry  .  getKey  ();      }      ans     *=     sum  ;      }      return     ans  ;      }      // Driver code      public     static     void     main  (  String  []     args  )         {      int     n     =     10  ;      System  .  out  .  println  (  sumDivisorsOfDivisors  (  n  ));      }   }   // This code is contributed by   // sanjeev2552   
Python3
   # Python3 program to find sum of divisors    # of all the divisors of a natural number.   import   math   as   mt   # Returns sum of divisors of all    # the divisors of n   def   sumDivisorsOfDivisors  (  n  ):   # Calculating powers of prime factors    # and storing them in a map mp[].   mp   =   dict  ()   for   j   in   range  (  2     mt  .  ceil  (  mt  .  sqrt  (  n  ))):   count   =   0   while   (  n   %   j   ==   0  ):   n   //=   j   count   +=   1   if   (  count  ):   mp  [  j  ]   =   count   # If n is a prime number   if   (  n   !=   1  ):   mp  [  n  ]   =   1   # For each prime factor calculating    # (p^(a+1)-1)/(p-1) and adding it to answer.   ans   =   1   for   it   in   mp  :   pw   =   1   summ   =   0   for   i   in   range  (  mp  [  it  ]   +   1     0     -  1  ):   summ   +=   (  i   *   pw  )   pw   *=   it   ans   *=   summ   return   ans   # Driver Code   n   =   10   print  (  sumDivisorsOfDivisors  (  n  ))   # This code is contributed   # by mohit kumar 29   
C#
   // C# program to find sum of divisors of all    // the divisors of a natural number.    using     System  ;   using     System.Collections.Generic  ;          class     GFG      {      // Returns sum of divisors of       // all the divisors of n      public     static     int     sumDivisorsOfDivisors  (  int     n  )      {      // Calculating powers of prime factors and      // storing them in a map mp[].      Dictionary   <  int           int  >     mp     =     new     Dictionary   <  int        int  >  ();      for     (  int     j     =     2  ;     j      <=     Math  .  Sqrt  (  n  );     j  ++  )         {      int     count     =     0  ;      while     (  n     %     j     ==     0  )         {      n     /=     j  ;      count  ++  ;      }      if     (  count     !=     0  )      mp  .  Add  (  j       count  );      }      // If n is a prime number      if     (  n     !=     1  )      mp  .  Add  (  n       1  );      // For each prime factor       // calculating (p^(a+1)-1)/(p-1)      // and adding it to answer.      int     ans     =     1  ;      foreach  (  KeyValuePair   <  int       int  >     entry     in     mp  )         {      int     pw     =     1  ;      int     sum     =     0  ;      for     (  int     i     =     entry  .  Value     +     1  ;         i     >=     1  ;     i  --  )      {      sum     +=     (  i     *     pw  );      pw     =     entry  .  Key  ;      }      ans     *=     sum  ;      }      return     ans  ;      }      // Driver code      public     static     void     Main  (  String  []     args  )         {      int     n     =     10  ;      Console  .  WriteLine  (  sumDivisorsOfDivisors  (  n  ));      }   }   // This code is contributed   // by Princi Singh   
JavaScript
    <  script  >   // Javascript program to find sum of divisors of all    // the divisors of a natural number.           // Returns sum of divisors of all the divisors      // of n      function     sumDivisorsOfDivisors  (  n  )      {      // Calculating powers of prime factors and      // storing them in a map mp[].      let     mp     =     new     Map  ();      for     (  let     j     =     2  ;     j      <=     Math  .  sqrt  (  n  );     j  ++  )         {      let     count     =     0  ;      while     (  n     %     j     ==     0  )         {      n     =     Math  .  floor  (  n  /  j  );      count  ++  ;      }      if     (  count     !=     0  )      mp  .  set  (  j       count  );      }          // If n is a prime number      if     (  n     !=     1  )      mp  .  set  (  n       1  );          // For each prime factor calculating (p^(a+1)-1)/(p-1)      // and adding it to answer.      let     ans     =     1  ;          for     (  let     [  key       value  ]     of     mp  .  entries  ())         {      let     pw     =     1  ;      let     sum     =     0  ;      for     (  let     i     =     value     +     1  ;     i     >=     1  ;     i  --  )      {      sum     +=     (  i     *     pw  );      pw     =     key  ;      }      ans     *=     sum  ;      }          return     ans  ;      }          // Driver code      let     n     =     10  ;      document  .  write  (  sumDivisorsOfDivisors  (  n  ));           // This code is contributed by patel2127    <  /script>   

Ausgabe:  

28 

Zeitkomplexität: O(?n log n) 
Hilfsraum: An)

Optimierungen: 
Für den Fall, dass es mehrere Eingaben gibt, für die wir den Wert ermitteln müssen, den wir verwenden können Sieb des Eratosthenes wie in besprochen Das Post.


 

Quiz erstellen

Das Könnte Ihnen Gefallen

Top Artikel

Kategorie

Interessante Artikel