Skip to content
Related Articles

Related Articles

Find the sum of product of power and LCM for N operations

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 19 Apr, 2022
View Discussion
Improve Article
Save Article

Given an integer N, the task is to find the value after performing the following N operation when i is 1 initially:

  • Find the value of i raised to the power of (N – i) and then multiply it with the lcm of i and N.
  • Add this value to the final answer. 
  • Increment i by 1.

Examples:

 Input: 5
 Output: 305
 Explanation: Following are the steps of the operations:
pow(1, 5-1) * (lcm(1, 5)) = 1 * 5
pow(2, 5-2) * (lcm(2, 5)) = 8 * 10 = 80
pow(3, 5-3) * (lcm(3, 5)) = 9 * 15 = 135
pow(4, 5-4) * (lcm(4, 5)) = 4 * 20 = 80
pow(5, 5-5) * (lcm(5, 5)) = 1 * 5 = 5
Hence the final sum is: 5 + 80 + 135 + 80 + 5 = 305

Input: 6
Output: 612

 

Approach: This is a simple implementation based problem. The idea is to perform the steps one by one as mentioned and find the final sum. Follow the steps mentioned below to solve the problem:

  • Start traversing i from i = 1 to N
    • Calculate the LCM of i and N.
    • Calculate the value of i raised to the power of N-i.
    • Keep multiplying the value of the above two steps and add it to the final sum (initially sum will be 0).  
  • Return the sum.

Below is the implementation of the above approach:

C++




// C++ program to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// GCD function
int gcd(int x, int y)
{
    if (x == 0)
        return y;
    return gcd(y % x, x);
}
 
// Function to find sum
int findsum(int N)
{
    int i, hcf, sum = 0, lcm;
 
    for (i = 1; i <= N; i++) {
 
        // Returning the gcd of two numbers
        hcf = gcd(i, N);
 
        // Calculating lcm using lcm*hcf=
        // value1*value2.
        lcm = (i * N) / hcf;
        sum += pow(i, N - i) * lcm;
    }
    return sum;
}
 
// Driver function
int main()
{
    int N = 5;
 
    // Function call
    cout << findsum(N) << endl;
    return 0;
}


Java




// Java program to implement the approach
import java.io.*;
 
class GFG
{
 
  // GCD function
  public static int gcd(int x, int y)
  {
    if (x == 0)
      return y;
    return gcd(y % x, x);
  }
 
  // Function to find sum
  public static int findsum(int N)
  {
    int i = 0, hcf = 0, sum = 0, lcm = 0;
 
    for (i = 1; i <= N; i++) {
 
      // Returning the gcd of two numbers
      hcf = gcd(i, N);
 
      // Calculating lcm using lcm*hcf=
      // value1*value2.
      lcm = (i * N) / hcf;
      sum += Math.pow(i, N - i) * lcm;
    }
    return sum;
  }
  public static void main(String[] args)
  {
    int N = 5;
 
    // Function call
    System.out.println(findsum(N));
  }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Python program to implement the approach
 
# GCD function
def gcd(x, y):
 
    if (x == 0):
        return y
    return gcd(y % x, x)
 
# Function to find sum
def findsum(N):
    hcf = 0
    sum1 = 0
    lcm = 0
 
    for i in range(1, N+1):
 
        # Returning the gcd of two numbers
        hcf = gcd(i, N)
 
        # Calculating lcm using lcm*hcf=
        # value1*value2.
        lcm = (int)((i * N) / hcf)
        sum1 = sum1 + (int)(pow(i, N - i) * lcm)
 
    return sum1
 
# Driver function
N = 5
 
# Function call
print(findsum(N))
 
# This code is contributed by Taranpreet


C#




// C# program to implement the approach
using System;
 
public class GFG{
 
  // GCD function
  public static int gcd(int x, int y)
  {
    if (x == 0)
      return y;
    return gcd(y % x, x);
  }
 
  // Function to find sum
  public static int findsum(int N)
  {
    int i = 0, hcf = 0, sum = 0, lcm = 0;
 
    for (i = 1; i <= N; i++) {
 
      // Returning the gcd of two numbers
      hcf = gcd(i, N);
 
      // Calculating lcm using lcm*hcf=
      // value1*value2.
      lcm = (i * N) / hcf;
      sum += (int)Math.Pow(i, N - i) * (int)lcm;
    }
    return sum;
  }
 
  // Driver Code
  static public void Main ()
  {
    int N = 5;
 
    // Function call
    Console.Write(findsum(N));
  }
}
 
// This code is contributed by hrithikgarg03188.


Javascript




<script>
    // JavaScript program to implement the approach
 
    // GCD function
    const gcd = (x, y) => {
        if (x == 0)
            return y;
        return gcd(y % x, x);
    }
 
    // Function to find sum
    const findsum = (N) => {
        let i, hcf, sum = 0, lcm;
 
        for (i = 1; i <= N; i++) {
 
            // Returning the gcd of two numbers
            hcf = gcd(i, N);
 
            // Calculating lcm using lcm*hcf=
            // value1*value2.
            lcm = parseInt((i * N) / hcf);
            sum += Math.pow(i, N - i) * lcm;
        }
        return sum;
    }
 
    // Driver function
    let N = 5;
 
    // Function call
    document.write(findsum(N));
 
// This code is contributed by rakeshsahni
 
</script>


Output

305

Time Complexity: O(N * log N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!