Find the sum of numbers from 1 to n excluding those which are powers of K
Last Updated :
19 Sep, 2023
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.
- Initialize a variable sum to store the sum of all the elements from the range [1, n] excluding those which are powers of k.
- Iterate through the range [1, n] using a for loop.
- Check if the current element i is a power of k or not. This can be done by calculating log(i) / log(k) and checking if the result is not an integer, i.e., if log(i) / log(k) != floor(log(i) / log(k)).
- If the current element i is not a power of k, add it to the sum.
- After the loop, return the required sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll getSum(ll n, ll k)
{
ll sum = 0;
for (ll i = 1; i <= n; i++) {
if ( log (i) / log (k) != floor ( log (i) / log (k))) {
sum += i;
}
}
return sum;
}
int main()
{
ll n = 10, k = 3;
cout << getSum(n, k);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
long n = 10 , k = 3 ;
System.out.println(getSum(n, k));
}
public static long getSum( long n, long k) {
long sum = 0 ;
for ( long i = 1 ; i <= n; i++) {
if (Math.log(i) / Math.log(k) != Math.floor(Math.log(i) / Math.log(k))) {
sum += i;
}
}
return sum;
}
}
|
Python3
import math
def get_sum(n, k):
sum_val = 0
for i in range ( 1 , n + 1 ):
if math.log(i, k) ! = int (math.log(i, k)):
sum_val + = i
return sum_val
if __name__ = = "__main__" :
n = 10
k = 3
print (get_sum(n, k))
|
C#
using System;
namespace SumExcludingPowers
{
class GFG
{
static long GetSum( long n, long k)
{
long sum = 0;
for ( long i = 1; i <= n; i++)
{
if (Math.Log(i, k) != Math.Floor(Math.Log(i, k)))
{
sum += i;
}
}
return sum;
}
static void Main( string [] args)
{
long n = 10, k = 3;
Console.WriteLine(GetSum(n, k));
}
}
}
|
Javascript
function getSum(n, k) {
let sumVal = 0;
for (let i = 1; i <= n; i++) {
if (Math.log(i) / Math.log(k) !== parseInt(Math.log(i) / Math.log(k))) {
sumVal += i;
}
}
return sumVal;
}
const n = 10;
const k = 3;
console.log(getSum(n, k));
|
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:
- pwrK: The sum of all the powers of K from [1, N] i.e. K0 + K1 + K2 + … + Kr such that Kr ≤ N
- 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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll sumPowersK(ll n, ll k)
{
ll sum = 0, num = 1;
while (num <= n) {
sum += num;
num *= k;
}
return sum;
}
ll getSum(ll n, ll k)
{
ll pwrK = sumPowersK(n, k);
ll sumAll = (n * (n + 1)) / 2;
return (sumAll - pwrK);
}
int main()
{
ll n = 10, k = 3;
cout << getSum(n, k);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static long sumPowersK( long n, long k)
{
long sum = 0 , num = 1 ;
while (num <= n)
{
sum += num;
num *= k;
}
return sum;
}
static long getSum( long n, long k)
{
long pwrK = sumPowersK(n, k);
long sumAll = (n * (n + 1 )) / 2 ;
return (sumAll - pwrK);
}
public static void main (String[] args)
{
long n = 10 , k = 3 ;
System.out.println( getSum(n, k));
}
}
|
Python3
def sumPowersK(n, k) :
sum = 0 ; num = 1 ;
while (num < = n) :
sum + = num;
num * = k;
return sum ;
def getSum(n, k) :
pwrK = sumPowersK(n, k);
sumAll = (n * (n + 1 )) / 2 ;
return (sumAll - pwrK);
if __name__ = = "__main__" :
n = 10 ; k = 3 ;
print (getSum(n, k));
|
C#
using System;
class GFG
{
static long sumPowersK( long n, long k)
{
long sum = 0, num = 1;
while (num <= n)
{
sum += num;
num *= k;
}
return sum;
}
static long getSum( long n, long k)
{
long pwrK = sumPowersK(n, k);
long sumAll = (n * (n + 1)) / 2;
return (sumAll - pwrK);
}
public static void Main ()
{
long n = 10, k = 3;
Console.WriteLine( getSum(n, k));
}
}
|
Javascript
<script>
function sumPowersK(n , k) {
var sum = 0, num = 1;
while (num <= n) {
sum += num;
num *= k;
}
return sum;
}
function getSum(n , k) {
var pwrK = sumPowersK(n, k);
var sumAll = (n * (n + 1)) / 2;
return (sumAll - pwrK);
}
var n = 10, k = 3;
document.write(getSum(n, k));
</script>
|
Time Complexity: O(logkN)
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...