Given two positive integers **N **and **K**, the task is to find the sum of the quotients of division of **N **by powers of **K** which are less than or equal to **N**.

**Examples:**

Input:N = 10, K = 2Output:18Explanation:

Dividing 10 by 1 (= 2^{0}). Quotient = 10. Therefore, sum = 10.

Dividing 10 by 2 (= 2^{1}). Quotient = 5. Therefore, sum = 15.

Divide 10 by 4 (= 2^{2}). Quotient = 2. Therefore, sum = 17.

Divide 10 by 8 (= 2^{3}). Quotient = 1. Therefore, sum = 18.

Input:N = 5, K=2Output:8Explanation:

Dividing 5 by 1 (= 2^{0}). Quotient = 5. Therefore, sum = 5.

Divide 5 by 2 (= 2^{1}). Quotient = 2. Therefore, sum = 7.

Divide 5 by 4 (= 2^{2}). Quotient = 1. Therefore, sum = 8.

**Approach: **The idea is to iterate a loop while the current power of **K** is less than or equal to **N** and keep adding the quotient to the sum in each iteration.

Follow the steps below to solve the problem:

- Initialize a variable, say
**sum**, to store the required sum. - Initialize a variable, say
**i**=**1**(= K^{0}) to store the powers of**K**. - Iterate until the value of
**i ≤ N**, and perform the following operations:- Store the quotient obtained on dividing
**N**by**i**in a variable, say**X**. - Add the value of
**X**to**ans**and multiply**i**by**K**to obtain the next power of**K**.

- Store the quotient obtained on dividing
- Print the value of the
**sum**as the result.

Below is the implementation of the above approach:

## C++

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

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG` `{` ` ` `// Function to calculate sum of` ` ` `// quotients obtained by dividing` ` ` `// N by powers of K <= N` ` ` `static` `void` `findSum(` `int` `N, ` `int` `K)` ` ` `{` ` ` `// Store the required sum` ` ` `int` `ans = ` `0` `;` ` ` `int` `i = ` `1` `;` ` ` `// Iterate until i exceeds N` ` ` `while` `(i <= N)` ` ` `{` ` ` `// Update sum` ` ` `ans += N / i;` ` ` `// Multiply i by K to` ` ` `// obtain next power of K` ` ` `i = i * K;` ` ` `}` ` ` `// Print the result` ` ` `System.out.println(ans);` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `// Given N and K` ` ` `int` `N = ` `10` `, K = ` `2` `;` ` ` `findSum(N, K);` ` ` `}` `}` `// This code is contributed by shubhamsingh10` |

## Python3

`# Python3 program for the above approach` `# Function to calculate sum of` `# quotients obtained by dividing` `# N by powers of K <= N` `def` `findSum(N, K):` ` ` ` ` `# Store the required sum` ` ` `ans ` `=` `0` ` ` `i ` `=` `1` ` ` `# Iterate until i exceeds N` ` ` `while` `(i <` `=` `N):` ` ` `# Update sum` ` ` `ans ` `+` `=` `N ` `/` `/` `i` ` ` `# Multiply i by K to` ` ` `# obtain next power of K` ` ` `i ` `=` `i ` `*` `K` ` ` `# Prthe result` ` ` `print` `(ans)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given N and K` ` ` `N, K ` `=` `10` `, ` `2` ` ` `findSum(N, K)` ` ` `# This code is contributed by mohit kumar 29.` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` ` ` `// Function to calculate sum of` `// quotients obtained by dividing` `// N by powers of K <= N` `static` `void` `findSum(` `int` `N, ` `int` `K)` `{` ` ` `// Store the required sum` ` ` `int` `ans = 0;` ` ` `int` `i = 1;` ` ` ` ` `// Iterate until i exceeds N` ` ` `while` `(i <= N)` ` ` `{` ` ` ` ` `// Update sum` ` ` `ans += N / i;` ` ` ` ` `// Multiply i by K to` ` ` `// obtain next power of K` ` ` `i = i * K;` ` ` `}` ` ` ` ` `// Print the result` ` ` `Console.Write(ans);` `}` `// Driver code` `static` `void` `Main()` `{` ` ` ` ` `// Given N and K` ` ` `int` `N = 10, K = 2;` ` ` ` ` `findSum(N, K);` `}` `}` `// This code is contributed by code_hunt` |

**Output:**

18

**Time Complexity: **O(log_{K}(N))**Auxiliary Space: **O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.