Given two integers and , the task is to find the sum of all the numbers within the range **[1, n]** excluding the numbers which are positive powers of **k** i.e. the numbers **k, k ^{2}, k^{3} and so on**.

**Examples:**

Input:n = 10, k = 3

Output:43

1 + 2 + 4 + 5 + 6 + 7 + 8 + 10 = 43

3 and 9 are excluded as they are powers of 3

Input:n = 11, k = 2

Output:52

**Approach:**

- Store the sum of first natural numbers in a variable i.e.
**sum = (n * (n + 1)) / 2**. - Now for every positive power of which is less than , subtract it from the .
- Print the value of the variable in the end.

Below is the implementation of the above approach:

## C++

`// C++ program to find the sum of ` `// first n natural numbers which are ` `// not positive powers of k ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the sum of ` `// first n natural numbers which are ` `// not positive powers of k ` `int` `find_sum(` `int` `n, ` `int` `k) ` `{ ` ` ` `// sum of first n natural numbers ` ` ` `int` `total_sum = (n * (n + 1)) / 2; ` ` ` ` ` `int` `power = k; ` ` ` `while` `(power <= n) { ` ` ` ` ` `// subtract all positive powers ` ` ` `// of k which are less than n ` ` ` `total_sum -= power; ` ` ` ` ` `// next power of k ` ` ` `power *= k; ` ` ` `} ` ` ` ` ` `return` `total_sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 11, k = 2; ` ` ` ` ` `cout << find_sum(n, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the sum of ` `// first n natural numbers which are ` `// not positive powers of k ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` ` ` `// Function to return the sum of ` `// first n natural numbers which are ` `// not positive powers of k ` `static` `int` `find_sum(` `int` `n, ` `int` `k) ` `{ ` ` ` `// sum of first n natural numbers ` ` ` `int` `total_sum = (n * (n + ` `1` `)) / ` `2` `; ` ` ` ` ` `int` `power = k; ` ` ` `while` `(power <= n) { ` ` ` ` ` `// subtract all positive powers ` ` ` `// of k which are less than n ` ` ` `total_sum -= power; ` ` ` ` ` `// next power of k ` ` ` `power *= k; ` ` ` `} ` ` ` ` ` `return` `total_sum; ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `11` `, k = ` `2` `; ` ` ` ` ` `System.out.println(find_sum(n, k)); ` ` ` `} ` `} ` `// This code is contributed by inder_verma.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find the sum of ` `# first n natural numbers which are ` `# not positive powers of k ` ` ` `# Function to return the sum of ` `# first n natural numbers which are ` `# not positive powers of k ` `def` `find_sum(n, k): ` ` ` ` ` `# sum of first n natural numbers ` ` ` `total_sum ` `=` `(n ` `*` `(n ` `+` `1` `)) ` `/` `/` `2` ` ` `power ` `=` `k ` ` ` `while` `power <` `=` `n: ` ` ` ` ` `# subtract all positive powers ` ` ` `# of k which are less than n ` ` ` `total_sum ` `-` `=` `power ` ` ` ` ` `# next power of k ` ` ` `power ` `*` `=` `k ` ` ` `return` `total_sum ` ` ` `# Driver code ` `n ` `=` `11` `; k ` `=` `2` `print` `(find_sum(n, k)) ` ` ` `# This code is contributed ` `# by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the sum of ` `// first n natural numbers which are ` `// not positive powers of k ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` ` ` `// Function to return the sum of ` `// first n natural numbers which are ` `// not positive powers of k ` `static` `int` `find_sum(` `int` `n, ` `int` `k) ` `{ ` ` ` `// sum of first n natural numbers ` ` ` `int` `total_sum = (n * (n + 1)) / 2; ` ` ` ` ` `int` `power = k; ` ` ` `while` `(power <= n) { ` ` ` ` ` `// subtract all positive powers ` ` ` `// of k which are less than n ` ` ` `total_sum -= power; ` ` ` ` ` `// next power of k ` ` ` `power *= k; ` ` ` `} ` ` ` ` ` `return` `total_sum; ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `Main () { ` ` ` `int` `n = 11, k = 2; ` ` ` ` ` `Console.WriteLine(find_sum(n, k)); ` ` ` `} ` `} ` `// This code is contributed by inder_verma.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the sum of ` `// first n natural numbers which are ` `// not positive powers of k ` ` ` `// Function to return the sum of ` `// first n natural numbers which are ` `// not positive powers of k ` `function` `find_sum(` `$n` `, ` `$k` `) ` `{ ` ` ` `// sum of first n natural numbers ` ` ` `$total_sum` `= (` `$n` `* (` `$n` `+ 1)) / 2; ` ` ` ` ` `$power` `= ` `$k` `; ` ` ` `while` `(` `$power` `<= ` `$n` `) ` ` ` `{ ` ` ` ` ` `// subtract all positive powers ` ` ` `// of k which are less than n ` ` ` `$total_sum` `-= ` `$power` `; ` ` ` ` ` `// next power of k ` ` ` `$power` `*= ` `$k` `; ` ` ` `} ` ` ` ` ` `return` `$total_sum` `; ` `} ` ` ` `// Driver code ` `$n` `= 11; ` `$k` `= 2; ` ` ` `echo` `find_sum(` `$n` `, ` `$k` `); ` ` ` `// This code is contributed by inder_verma.. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

52

## Recommended Posts:

- Sum of fifth powers of the first n natural numbers
- Sum of fourth powers of first n odd natural numbers
- Sum of fourth powers of the first n natural numbers
- Sum of first N natural numbers with all powers of 2 added twice
- Sum of first N natural numbers by taking powers of 2 as negative number
- Find k numbers which are powers of 2 and have sum N | Set 1
- Find the sum of numbers from 1 to n excluding those which are powers of K
- Print all integers that are sum of powers of two given numbers
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Sum of first n natural numbers
- LCM of First n Natural Numbers
- Natural Numbers
- Average of first n even natural numbers
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum of all natural numbers in range L to R
- Sum of cubes of even and odd natural numbers
- Sum of first N natural numbers which are divisible by X or Y
- Sum of all odd natural numbers in range L and R

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.