Find the sum of product of power and LCM for N operations
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++
#include <bits/stdc++.h>
using namespace std;
int gcd( int x, int y)
{
if (x == 0)
return y;
return gcd(y % x, x);
}
int findsum( int N)
{
int i, hcf, sum = 0, lcm;
for (i = 1; i <= N; i++) {
hcf = gcd(i, N);
lcm = (i * N) / hcf;
sum += pow (i, N - i) * lcm;
}
return sum;
}
int main()
{
int N = 5;
cout << findsum(N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static int gcd( int x, int y)
{
if (x == 0 )
return y;
return gcd(y % x, x);
}
public static int findsum( int N)
{
int i = 0 , hcf = 0 , sum = 0 , lcm = 0 ;
for (i = 1 ; i <= N; i++) {
hcf = gcd(i, N);
lcm = (i * N) / hcf;
sum += Math.pow(i, N - i) * lcm;
}
return sum;
}
public static void main(String[] args)
{
int N = 5 ;
System.out.println(findsum(N));
}
}
|
Python3
def gcd(x, y):
if (x = = 0 ):
return y
return gcd(y % x, x)
def findsum(N):
hcf = 0
sum1 = 0
lcm = 0
for i in range ( 1 , N + 1 ):
hcf = gcd(i, N)
lcm = ( int )((i * N) / hcf)
sum1 = sum1 + ( int )( pow (i, N - i) * lcm)
return sum1
N = 5
print (findsum(N))
|
C#
using System;
public class GFG{
public static int gcd( int x, int y)
{
if (x == 0)
return y;
return gcd(y % x, x);
}
public static int findsum( int N)
{
int i = 0, hcf = 0, sum = 0, lcm = 0;
for (i = 1; i <= N; i++) {
hcf = gcd(i, N);
lcm = (i * N) / hcf;
sum += ( int )Math.Pow(i, N - i) * ( int )lcm;
}
return sum;
}
static public void Main ()
{
int N = 5;
Console.Write(findsum(N));
}
}
|
Javascript
<script>
const gcd = (x, y) => {
if (x == 0)
return y;
return gcd(y % x, x);
}
const findsum = (N) => {
let i, hcf, sum = 0, lcm;
for (i = 1; i <= N; i++) {
hcf = gcd(i, N);
lcm = parseInt((i * N) / hcf);
sum += Math.pow(i, N - i) * lcm;
}
return sum;
}
let N = 5;
document.write(findsum(N));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Last Updated :
19 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...