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
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++
#include <bits/stdc++.h>
using namespace std;
void powerfulIntegers( int x, int y, int bound)
{
set< int > s;
vector< int > powersOfY;
int i;
powersOfY.push_back(1);
for (i = y; i < bound && y!= 1; i = i * y)
powersOfY.push_back(i);
i = 0;
while ( true ) {
int xPowI = pow (x, i);
for ( auto j = powersOfY.begin(); j != powersOfY.end(); ++j) {
int num = xPowI + *j;
if (num <= bound)
s.insert(num);
else
break ;
}
if (xPowI >= bound || x == 1)
break ;
i++;
}
set< int >::iterator itr;
for (itr = s.begin(); itr != s.end(); itr++) {
cout << *itr << " " ;
}
}
int main()
{
int x = 2, y = 3, bound = 10;
powerfulIntegers(x, y, bound);
return 0;
}
|
Java
import java.util.*;
import java.lang.Math;
class GfG
{
static void powerfulIntegers( int x,
int y, int bound)
{
Set<Integer> s = new HashSet<>();
ArrayList<Integer> powersOfY = new ArrayList<>();
int i;
powersOfY.add( 1 );
for (i = y; i < bound && y != 1 ; i = i * y)
powersOfY.add(i);
i = 0 ;
while ( true )
{
int xPowI = ( int )Math.pow(( double )x, ( double )i);
for ( int j = 0 ; j < powersOfY.size(); ++j)
{
int num = xPowI + powersOfY.get(j);
if (num <= bound)
s.add(num);
else
break ;
}
if (xPowI >= bound || x == 1 )
break ;
i++;
}
Iterator itr = s.iterator();
while (itr.hasNext())
{
System.out.print(itr.next() + " " );
}
}
public static void main(String []args)
{
int x = 2 , y = 1 , bound = 10 ;
powerfulIntegers(x, y, bound);
}
}
|
Python3
def powerfulIntegers(x, y, bound) :
s = set ()
powersOfY = []
powersOfY.append( 1 )
i = y
while i < bound and y! = 1 :
powersOfY.append(i)
i * = y
i = 0
while ( True ) :
xPowI = pow (x, i)
for j in powersOfY :
num = xPowI + j
if (num < = bound) :
s.add(num)
else :
break
if (xPowI > = bound or x = = 1 ) :
break
i + = 1
for itr in s :
print (itr, end = " " )
if __name__ = = "__main__" :
x = 2
y = 3
bound = 10
powerfulIntegers(x, y, bound)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
class GFG
{
static void powerfulIntegers( int x, int y, int bound)
{
HashSet< int > s = new HashSet< int >();
ArrayList powersOfY = new ArrayList();
int i;
powersOfY.Add(1);
for (i = y; i < bound && y != 1; i = i * y)
powersOfY.Add(i);
i = 0;
while ( true )
{
int xPowI = ( int )Math.Pow(x, i);
for ( int j = 0; j != powersOfY.Count; ++j)
{
int num = xPowI + ( int )powersOfY[j];
if (num <= bound)
s.Add(num);
else
break ;
}
if (xPowI >= bound || x == 1)
break ;
i++;
}
int [] ar = s.ToArray();
Array.Sort(ar);
s.Clear();
s.UnionWith(ar);
foreach ( int t in s)
{
Console.Write( t + " " );
}
}
static void Main()
{
int x = 2, y = 3, bound = 10;
powerfulIntegers(x, y, bound);
}
}
|
PHP
<?php
function powerfulIntegers( $x , $y , $bound )
{
$s = array ();
$powersOfY = array ();
array_push ( $powersOfY , 1);
$i = $y ;
while ( $i < $bound && $y != 1)
{
array_push ( $powersOfY , $i );
$i *= $y ;
}
$i = 0;
while (true)
{
$xPowI = pow( $x , $i );
for ( $j = 0; $j < count ( $powersOfY ); $j ++)
{
$num = $xPowI + $powersOfY [ $j ];
if ( $num <= $bound )
array_push ( $s , $num );
else
break ;
}
if ( $xPowI >= $bound || $x == 1)
break ;
$i += 1;
}
$s = array_unique ( $s );
sort( $s );
foreach ( $s as & $itr )
print ( $itr . " " );
}
$x = 2;
$y = 3;
$bound = 10;
powerfulIntegers( $x , $y , $bound );
?>
|
Javascript
<script>
function powerfulIntegers(x, y, bound)
{
var s = new Set();
var powersOfY = [];
var i;
powersOfY.push(1);
for (i = y; i < bound && y!= 1; i = i * y)
powersOfY.push(i);
i = 0;
while ( true )
{
var xPowI = Math.pow(x, i);
powersOfY.forEach(j => {
var num = xPowI + j;
if (num <= bound)
s.add(num);
});
if (xPowI >= bound || x == 1)
break ;
i++;
}
[...s].sort((a, b) => a - b).forEach(itr => {
document.write(itr + " " )
});
}
var x = 2, y = 3, bound = 10;
powerfulIntegers(x, y, bound);
</script>
|
Last Updated :
09 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...