Open In App

Find the sum of numbers from 1 to n excluding those which are powers of K

Given two integer N and K, the task is to find the sum of all the numbers from the range [1, N] excluding those which are powers of K.
 

Examples:

Input: N = 10, K = 3 
Output: 42 
2 + 4 + 5 + 6 + 7 + 8 + 10 = 42 
1, 3 and 9 are excluded as they are powers of 3.
Input: N = 200, K = 30 
Output: 20069  

Approach:

Approach to solve this problem could be iterating through the range [1, N] and checking if each number is a power of K or not. If it is not, then add it to the sum. This can be achieved using a loop and checking the value of each number raised to the power of K using the log function.

Below is the implementation of the above approach:




#include <bits/stdc++.h>
using namespace std;
#define ll long long int
 
// Function to return the sum of all the elements
// from the range [1, n] excluding those which are
// powers of k
ll getSum(ll n, ll k)
{
    // Variable to store the sum
    ll sum = 0;
 
    // Iterate through the range [1, n]
    for (ll i = 1; i <= n; i++) {
 
        // Check if i is a power of k or not
        if (log(i) / log(k) != floor(log(i) / log(k))) {
 
            // If i is not a power of k, add it to the sum
            sum += i;
        }
    }
 
    // Return the required sum
    return sum;
}
 
// Driver code
int main()
{
    ll n = 10, k = 3;
 
    cout << getSum(n, k);
 
    return 0;
}




import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        long n = 10, k = 3;
        System.out.println(getSum(n, k));
    }
 
    // Function to calculate the sum of numbers that do not have an exact base-k logarithm
    public static long getSum(long n, long k) {
        long sum = 0;
        for (long i = 1; i <= n; i++) {
            // Calculate the base-k logarithm of 'i' and check if it is not an integer value
            if (Math.log(i) / Math.log(k) != Math.floor(Math.log(i) / Math.log(k))) {
                // If the logarithm is not an integer, add 'i' to the sum
                sum += i;
            }
        }
        return sum;
    }
}




import math
 
# Function to return the sum of all the elements
# from the range [1, n] excluding those which are
# powers of k
def get_sum(n, k):
    # Variable to store the sum
    sum_val = 0
 
    # Iterate through the range [1, n]
    for i in range(1, n + 1):
 
        # Check if i is a power of k or not
        if math.log(i, k) != int(math.log(i, k)):
 
            # If i is not a power of k, add it to the sum
            sum_val += i
 
    # Return the required sum
    return sum_val
 
# Driver code
if __name__ == "__main__":
    n = 10
    k = 3
 
    print(get_sum(n, k))




using System;
 
namespace SumExcludingPowers
{
    class GFG
    {
        // Function to return the sum of all the elements
        // from the range [1, n] excluding those which are
        // powers of k
        static long GetSum(long n, long k)
        {
            // Variable to store the sum
            long sum = 0;
 
            // Iterate through the range [1, n]
            for (long i = 1; i <= n; i++)
            {
                // Check if i is a power of k or not
                if (Math.Log(i, k) != Math.Floor(Math.Log(i, k)))
                {
                    // If i is not a power of k, add it to the sum
                    sum += i;
                }
            }
 
            // Return the required sum
            return sum;
        }
 
        // Main function
        static void Main(string[] args)
        {
            long n = 10, k = 3;
 
            Console.WriteLine(GetSum(n, k));
        }
    }
}




// Function to return the sum of all the elements
// from the range [1, n] excluding those which are
// powers of k
function getSum(n, k) {
    // Variable to store the sum
    let sumVal = 0;
 
    // Iterate through the range [1, n]
    for (let i = 1; i <= n; i++) {
        // Check if i is a power of k or not
        if (Math.log(i) / Math.log(k) !== parseInt(Math.log(i) / Math.log(k))) {
            // If i is not a power of k, add it to the sum
            sumVal += i;
        }
    }
 
    // Return the required sum
    return sumVal;
}
 
// Driver code
const n = 10;
const k = 3;
 
console.log(getSum(n, k));

Output
42







Time Complexity: O(n log(n)) because for each element in the range [1,n], we are computing log(i) which takes O(log(i)) time. 
Space Complexity: O(1) because we are only using a constant amount of extra space to store the sum variable.

Approach: Find the sum of the following series:  

  1. pwrK: The sum of all the powers of K from [1, N] i.e. K0 + K1 + K2 + … + Kr such that Kr ≤ N
  2. sumAll: The sum of all the integers from the range [1, N] i.e. (N * (N + 1)) / 2.

The result will be sumAll – pwrK
Below is the implementation of the above approach: 
 




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
 
// Function to return the sum of all the
// powers of k from the range [1, n]
ll sumPowersK(ll n, ll k)
{
 
    // To store the sum of the series
    ll sum = 0, num = 1;
 
    // While current power of k <= n
    while (num <= n) {
 
        // Add current power to the sum
        sum += num;
 
        // Next power of k
        num *= k;
    }
 
    // Return the sum of the series
    return sum;
}
 
// Find to return the sum of the
// elements from the range [1, n]
// excluding those which are powers of k
ll getSum(ll n, ll k)
{
    // Sum of all the powers of k from [1, n]
    ll pwrK = sumPowersK(n, k);
 
    // Sum of all the elements from [1, n]
    ll sumAll = (n * (n + 1)) / 2;
 
    // Return the required sum
    return (sumAll - pwrK);
}
 
// Driver code
int main()
{
    ll n = 10, k = 3;
 
    cout << getSum(n, k);
 
    return 0;
}




// Java implementation of the approach
import java.io.*;
 
class GFG
{
 
// Function to return the sum of all the
// powers of k from the range [1, n]
static long sumPowersK(long n, long k)
{
 
    // To store the sum of the series
    long sum = 0, num = 1;
 
    // While current power of k <= n
    while (num <= n)
    {
 
        // Add current power to the sum
        sum += num;
 
        // Next power of k
        num *= k;
    }
 
    // Return the sum of the series
    return sum;
}
 
// Find to return the sum of the
// elements from the range [1, n]
// excluding those which are powers of k
static long getSum(long n, long k)
{
    // Sum of all the powers of k from [1, n]
    long pwrK = sumPowersK(n, k);
 
    // Sum of all the elements from [1, n]
    long sumAll = (n * (n + 1)) / 2;
 
    // Return the required sum
    return (sumAll - pwrK);
}
 
    // Driver code
    public static void main (String[] args)
    {
        long n = 10, k = 3;
        System.out.println( getSum(n, k));
 
    }
}
 
// This code is contributed by anuj_67..




# Python3 implementation of the approach
 
# Function to return the sum of all the
# powers of k from the range [1, n]
def sumPowersK(n, k) :
 
    # To store the sum of the series
    sum = 0; num = 1;
 
    # While current power of k <= n
    while (num <= n) :
 
        # Add current power to the sum
        sum += num;
 
        # Next power of k
        num *= k;
 
    # Return the sum of the series
    return sum;
     
 
# Find to return the sum of the
# elements from the range [1, n]
# excluding those which are powers of k
def getSum(n, k) :
 
    # Sum of all the powers of k from [1, n]
    pwrK = sumPowersK(n, k);
 
    # Sum of all the elements from [1, n]
    sumAll = (n * (n + 1)) / 2;
 
    # Return the required sum
    return (sumAll - pwrK);
 
 
# Driver code
if __name__ == "__main__" :
 
    n = 10; k = 3;
 
    print(getSum(n, k));
     
# This code is contributed by AnkitRai01




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the sum of all the
// powers of k from the range [1, n]
static long sumPowersK(long n, long k)
{
 
    // To store the sum of the series
    long sum = 0, num = 1;
 
    // While current power of k <= n
    while (num <= n)
    {
 
        // Add current power to the sum
        sum += num;
 
        // Next power of k
        num *= k;
    }
 
    // Return the sum of the series
    return sum;
}
 
// Find to return the sum of the
// elements from the range [1, n]
// excluding those which are powers of k
static long getSum(long n, long k)
{
    // Sum of all the powers of k from [1, n]
    long pwrK = sumPowersK(n, k);
 
    // Sum of all the elements from [1, n]
    long sumAll = (n * (n + 1)) / 2;
 
    // Return the required sum
    return (sumAll - pwrK);
}
 
// Driver code
public static void Main ()
{
    long n = 10, k = 3;
    Console.WriteLine( getSum(n, k));
 
}
}
 
// This code is contributed by anuj_67..




<script>
// javascript implementation of the approach
 
    // Function to return the sum of all the
    // powers of k from the range [1, n]
    function sumPowersK(n , k) {
 
        // To store the sum of the series
        var sum = 0, num = 1;
 
        // While current power of k <= n
        while (num <= n) {
 
            // Add current power to the sum
            sum += num;
 
            // Next power of k
            num *= k;
        }
 
        // Return the sum of the series
        return sum;
    }
 
    // Find to return the sum of the
    // elements from the range [1, n]
    // excluding those which are powers of k
    function getSum(n , k) {
        // Sum of all the powers of k from [1, n]
        var pwrK = sumPowersK(n, k);
 
        // Sum of all the elements from [1, n]
        var sumAll = (n * (n + 1)) / 2;
 
        // Return the required sum
        return (sumAll - pwrK);
    }
 
    // Driver code
     
        var n = 10, k = 3;
        document.write(getSum(n, k));
 
 
// This code contributed by Rajput-Ji
</script>

Output
42







Time Complexity: O(logkN)
Auxiliary Space: O(1), since no extra space has been taken.


Article Tags :