
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all integers that are sum of powers of two given numbers in C++
In this problem, we are given two numbers a and b and an integer bound and we have to print all values less than binding which is the sum of squares of a and b.
Bound >= ai + bj
Let’s take an example to understand the problem −
Input: a=2, b=3, bound=8 Output: 2 3 4 5 7
To solve this problem, we will use nested loops using two variables i and j from 0. The outer loop will have ending condition xi = bound and the inner loop will have ending condition xi + yj > bound. For each iteration of the inner loop, we will store the value of xi + yi in a sorted list contains all such values. And then at the end print all values of the list.
Example
Program to show the implementation of our solution −
#include <bits/stdc++.h> using namespace std; void powerSum(int x, int y, int bound) { set<int> sumOfPowers; vector<int> powY; int i; powY.push_back(1); for (i = y; i < bound; i = i * y) powY.push_back(i); i = 0; while (true) { int powX = pow(x, i); if (powX >= bound) break; for (auto j = powY.begin(); j != powY.end(); ++j) { int num = powX + *j; if (num <= bound) sumOfPowers.insert(num); else break; } i++; } set<int>::iterator itr; for (itr = sumOfPowers.begin(); itr != sumOfPowers.end(); itr++) { cout<<*itr <<" "; } } int main() { int x = 2, y = 3, bound = 25; cout<<"Sum of powers of "<<x<<" and "<<y<<" less than "<<bound<<" are :\n"; powerSum(x, y, bound); return 0; }
Output
Sum of powers of 2 and 3 less than 25 are − 2 3 4 5 7 9 10 11 13 17 19 25
- Related Articles
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- How to get the sum of the powers of all numbers in JavaScript?
- Print all interleavings of given two strings in C++
- Print all pairs with given sum in C++
- Print all triplets with given sum in C++
- Find k numbers which are powers of 2 and have sum N in C++
- Print all possible sums of consecutive numbers with sum N in C++
- Print all prime factors and their powers in C++
- Print all Good numbers in given range in C++
- Balance pans using given weights that are powers of a number in C++ program
- Find two numbers whose sum and GCD are given in C++
- Sum of Two Integers in Python
- Print all sequences of given length in C++
- Print all combinations of points that can compose a given number in C++

Advertisements