Sum of first N natural numbers which are not powers of K | C ++ 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 ; sum of first n natural numbers ; subtract all positive powers of k which are less than n ; next power of k ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_sum ( int n , int k ) { int total_sum = ( n * ( n + 1 ) ) / 2 ; int power = k ; while ( power <= n ) { total_sum -= power ; power *= k ; } return total_sum ; } int main ( ) { int n = 11 , k = 2 ; cout << find_sum ( n , k ) ; return 0 ; }