# Print all integers that are sum of powers of two given numbers

Given three non-negative integers x, y and bound, the task is to print all the powerful integer ≤ bound in sorted order.
A powerful integer is of the form xi + yj for all i, j ≥ 0.

Examples:

Input: x = 3, y = 5, bound = 10
Output: 2 4 6 8 10
30 + 50 = 1 + 1 = 2
30 + 51 = 1 + 5 = 6
31 + 50 = 3 + 1 = 4
31 + 51 = 3 + 5 = 8
32 + 50 = 9 + 1 = 10

Input: x = 2, y = 3, bound = 10
Output: 2 3 4 5 7 9 10

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Initialize i = 0 for outer loop and j = 0 for inner loop, if xi = bound then break out of the outer loop (as adding any power of y to it will make it out of the bound). If xi + yj > bound then break out of the inner loop and in every other iteration of the inner loop, save xi + yj in a set to maintain a distinct and sorted list of the powerful integers. Print the contents of the set in the end.
To avoid calculating the powers of y again and again, all the powers of y can be pre-calculated and stored in a vector.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print powerful integers ` `void` `powerfulIntegers(``int` `x, ``int` `y, ``int` `bound) ` `{ ` ` `  `    ``// Set is used to store distinct numbers ` `    ``// in sorted order ` `    ``set<``int``> s; ` `    ``vector<``int``> powersOfY; ` `    ``int` `i; ` ` `  `    ``// Store all the powers of y < bound in a vector ` `    ``// to avoid calculating them again and again ` `    ``powersOfY.push_back(1); ` `    ``for` `(i = y; i < bound; i = i * y) ` `        ``powersOfY.push_back(i); ` ` `  `    ``i = 0; ` `    ``while` `(``true``) { ` ` `  `        ``// x^i ` `        ``int` `xPowI = ``pow``(x, i); ` ` `  `        ``// Adding any number to it ` `        ``// will be out of bounds ` `        ``if` `(xPowI >= bound) ` `            ``break``; ` ` `  `        ``for` `(``auto` `j = powersOfY.begin(); j != powersOfY.end(); ++j) { ` `            ``int` `num = xPowI + *j; ` ` `  `            ``// If num is within limits ` `            ``// insert it into the set ` `            ``if` `(num <= bound) ` `                ``s.insert(num); ` ` `  `            ``// Break out of the inner loop ` `            ``else` `                ``break``; ` `        ``} ` ` `  `        ``// Increment i ` `        ``i++; ` `    ``} ` ` `  `    ``// Print the contents of the set ` `    ``set<``int``>::iterator itr; ` `    ``for` `(itr = s.begin(); itr != s.end(); itr++) { ` `        ``cout << *itr << ``" "``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `x = 2, y = 3, bound = 10; ` ` `  `    ``// Print powerful integers ` `    ``powerfulIntegers(x, y, bound); ` `    ``return` `0; ` `} `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to print powerful integers  ` `def` `powerfulIntegers(x, y, bound) : ` ` `  `    ``# Set is used to store distinct  ` `    ``# numbers in sorted order  ` `    ``s ``=` `set``()  ` `    ``powersOfY ``=` `[] ` ` `  `    ``# Store all the powers of y < bound  ` `    ``# in a vector to avoid calculating  ` `    ``# them again and again  ` `    ``powersOfY.append(``1``) ` `    ``i ``=` `y ` `    ``while` `i < bound : ` `        ``powersOfY.append(i)  ` `        ``i ``*``=` `y ` ` `  `    ``i ``=` `0` `    ``while` `(``True``) : ` ` `  `        ``# x^i  ` `        ``xPowI ``=` `pow``(x, i) ` ` `  `        ``# Adding any number to it  ` `        ``# will be out of bounds  ` `        ``if` `(xPowI >``=` `bound) : ` `            ``break` ` `  `        ``for` `j ``in` `powersOfY :  ` `            ``num ``=` `xPowI ``+` `j ` ` `  `            ``# If num is within limits  ` `            ``# insert it into the set  ` `            ``if` `(num <``=` `bound) : ` `                ``s.add(num) ` ` `  `            ``# Break out of the inner loop  ` `            ``else` `: ` `                ``break` ` `  `        ``# Increment i  ` `        ``i ``+``=` `1` ` `  `    ``# Print the contents of the set  ` `    ``for` `itr ``in` `s :  ` `        ``print``(itr, end ``=` `" "``) ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``x ``=` `2` `    ``y ``=` `3` `    ``bound ``=` `10` ` `  `    ``# Print powerful integers  ` `    ``powerfulIntegers(x, y, bound) ` ` `  `# This code is contributed by Ryuga `

## PHP

= \$bound)
break;

for (\$j = 0; \$j < count(\$powersOfY); \$j++) { \$num = \$xPowI + \$powersOfY[\$j]; // If num is within limits // insert it into the set if (\$num <= \$bound) array_push(\$s, \$num); // Break out of the inner loop else break; } // Increment i \$i += 1; } \$s = array_unique(\$s); sort(\$s); // Print the contents of the set foreach (\$s as &\$itr) print(\$itr . " "); } // Driver code \$x = 2; \$y = 3; \$bound = 10; // Print powerful integers powerfulIntegers(\$x, \$y, \$bound); // This code is contributed by chandan_jnu ?>

Output:

```2 3 4 5 7 9 10
```

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.