Find the sum of numbers from 1 to n excluding those which are powers of K | C ++ implementation of the approach ; Function to return the sum of all the powers of k from the range [ 1 , n ] ; To store the sum of the series ; While current power of k <= n ; Add current power to the sum ; Next power of k ; Return the sum of the series ; Find to return the sum of the elements from the range [ 1 , n ] excluding those which are powers of k ; Sum of all the powers of k from [ 1 , n ] ; Sum of all the elements from [ 1 , n ] ; Return the required sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE 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 ; }