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.
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
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:
2 3 4 5 7 9 10
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Print all prime factors and their powers
- Print powers using Anonymous Function in Python
- Find k numbers which are powers of 2 and have sum N | Set 1
- Sum of first N natural numbers which are not powers of K
- Sum of fifth powers of the first n natural numbers
- Sum of fourth powers of first n odd natural numbers
- Find the sum of numbers from 1 to n excluding those which are powers of K
- Sum of fourth powers of the first n natural numbers
- Sum of first N natural numbers by taking powers of 2 as negative number
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Armstrong Numbers between two integers
- K-th smallest element after removing given integers from natural numbers | Set 2
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Median in a stream of integers (running integers)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.