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:** Find the sum of the following series:

**pwrK:**The sum of all the powers of**K**from**[1, N]**i.e.**K**such that^{0}+ K^{1}+ K^{2}+ … + K^{r}**K**^{r}≤ 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++

`// 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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// 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.. ` |

*chevron_right*

*filter_none*

## Python3

`# 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 ` |

*chevron_right*

*filter_none*

## C#

`// 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.. ` |

*chevron_right*

*filter_none*

**Output:**

42

## Recommended Posts:

- Find k numbers which are powers of 2 and have sum N | Set 1
- Sum of first N natural numbers which are not powers of K
- Calculate sum of all integers from 1 to N, excluding perfect power of 2
- Divide N into K unique parts such that gcd of those parts is maximum
- Count of maximum occurring subsequence using only those characters whose indices are in GP
- Sum of fourth powers of the first n natural numbers
- Sum of fifth powers of the first n natural numbers
- Sum of fourth powers of first n odd natural numbers
- Sum of first N natural numbers by taking powers of 2 as negative number
- Print all integers that are sum of powers of two given numbers
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Sum of first N natural numbers with all powers of 2 added twice
- Number of triangles possible with given lengths of sticks which are powers of 2
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Sum of largest divisible powers of p (a prime number) in a range
- Powers of 2 to required sum
- Represent n as the sum of exactly k powers of two | Set 2
- Check if a number can be represented as sum of non zero powers of 2
- Distinct powers of a number N such that the sum is equal to K
- Split N powers of 2 into two subsets such that their difference of sum is minimum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.