Sum of quotients of division of N by powers of K not exceeding N | C ++ program for the above approach ; Function to calculate sum of quotients obtained by dividing N by powers of K <= N ; Store the required sum ; Iterate until i exceeds N ; Update sum ; Multiply i by K to obtain next power of K ; Print the result ; Driver Code ; Given N and K
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int N , int K ) { int ans = 0 ; int i = 1 ; while ( i <= N ) { ans += N / i ; i = i * K ; } cout << ans ; } int main ( ) { int N = 10 , K = 2 ; findSum ( N , K ) ; return 0 ; }