Minimum number of power terms with sum equal to n
Last Updated :
12 Sep, 2023
Given two positive integer n and x. The task is to express n as sum of powers of x (xa1 + xa2 +.....+ xa3) such that the number of powers of x (xa1, xa2, ....., xa3) should be minimum. Print the minimum number of power of x used to make sum equal to n.
Examples:
Input : n = 5, x = 3
Output : 3
5 = 30 + 30 + 31.
We use only 3 power terms of x { 30, 30, 31}
Input : n = 13, x = 4
Output : 4
13 = 40 + 41 + 41 + 41.
We use only four power terms of x.
Input : n = 6, x = 1
Output : 6
If x = 1, then answer will be n only (n = 1 + 1 +.... n times).
The idea is to use Horner's method. Any number n can be expressed as, n = x * a + b where 0 <= b <= x-1. Now since b is between 0 to x - 1, then b should be expressed as sum of x0 b times.
Further a can be decomposed in similar manner and so on.
Algorithm to solve this problem:
1. Initialize a variable ans to 0.
2. While n > 0
a) ans = ans + n % x
b) n = n/x
3. Return ans.
Below is the implementation of above idea :
C++
// C++ program to calculate minimum number
// of powers of x to make sum equal to n.
#include <bits/stdc++.h>
using namespace std;
// Return minimum power terms of x required
int minPower(int n, int x)
{
// if x is 1, return n since any power
// of 1 is 1 only.
if (x==1)
return n;
// Consider n = a * x + b where a = n/x
// and b = n % x.
int ans = 0;
while (n > 0)
{
// Update count of powers for 1's added
ans += (n%x);
// Repeat the process for reduced n
n /= x;
}
return ans;
}
// Driven Program
int main()
{
int n = 5, x = 3;
cout << minPower(n, x) << endl;
return 0;
}
Java
// Java program to calculate
// minimum numberof powers of
// x to make sum equal to n.
class GFG
{
// Return minimum power
// terms of x required
static int minPower(int n, int x)
{
// if x is 1, return n since
// any power of 1 is 1 only.
if (x==1)
return n;
// Consider n = a * x + b where
// a = n/x and b = n % x.
int ans = 0;
while (n > 0)
{
// Update count of powers
// for 1's added
ans += (n % x);
// Repeat the process for reduced n
n /= x;
}
return ans;
}
// Driver code
public static void main (String[] args)
{
int n = 5, x = 3;
System.out.println(minPower(n, x));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to
# calculate minimum number
# of powers of x to make
# sum equal to n.
# Return minimum power
# terms of x required
def minPower(n,x):
# if x is 1, return
# n since any power
# of 1 is 1 only.
if (x==1):
return n
# Consider n = a * x + b where a = n/x
# and b = n % x.
ans = 0
while (n > 0):
# Update count of powers for 1's added
ans += (n%x)
# Repeat the process for reduced n
n //= x
return ans
# Driver code
n = 5
x = 3
print(minPower(n, x))
# This code is contributed
# by Anant Agarwal.
C#
// C# program to calculate
// minimum numberof powers
// of x to make sum equal
// to n.
using System;
class GFG
{
// Return minimum power
// terms of x required
static int minPower(int n, int x)
{
// if x is 1, return n since
// any power of 1 is 1 only.
if (x == 1)
return n;
// Consider n = a * x + b where
// a = n / x and b = n % x.
int ans = 0;
while (n > 0)
{
// Update count of
// powers for 1's
// added
ans += (n % x);
// Repeat the process
// for reduced n
n /= x;
}
return ans;
}
// Driver code
public static void Main ()
{
int n = 5, x = 3;
Console.WriteLine(minPower(n, x));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to calculate minimum number
// of powers of x to make sum equal to n.
// Return minimum power
// terms of x required
function minPower($n, $x)
{
// if x is 1, return n since
// any power of 1 is 1 only.
if ($x == 1)
return $n;
// Consider n = a * x + b
// where a = n/x and b = n % x.
$ans = 0;
while ($n > 0)
{
// Update count of powers
// for 1's added
$ans += ($n % $x);
// Repeat the process
// for reduced n
$n /= $x;
}
return $ans;
}
// Driver Code
$n = 5; $x = 3;
echo(minPower($n, $x));
// This code is contributed by Ajit.
?>
JavaScript
<script>
// JavaScript program to calculate minimum number
// of powers of x to make sum equal to n.
// Return minimum power terms of x required
function minPower(n, x)
{
// if x is 1, return n since any power
// of 1 is 1 only.
if (x==1)
return n;
// Consider n = a * x + b where a = n/x
// and b = n % x.
let ans = 0;
while (n > 0)
{
// Update count of powers for 1's added
ans += (n%x);
// Repeat the process for reduced n
n = Math.floor(n / x);
}
return ans;
}
// Driven Program
let n = 5, x = 3;
document.write(minPower(n, x) + "<br>");
// This code is contributed by Surbhi Tyagi.
</script>
Output:
3
Time complexity: O(logxn)
Auxiliary space:O(1)
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read