Number of integral solutions for equation x = b*(sumofdigits(x)^a)+c
Last Updated :
13 May, 2021
Given a, b and c which are part of the equation x = b * ( sumdigits(x) ^ a ) + c.
Where sumdigits(x) determines the sum of all digits of the number x. The task is to find out all integer solutions for x that satisfy the equation and print them in increasing order.
Given that, 1<=x<=109
Examples:
Input: a = 3, b = 2, c = 8
Output: 10 2008 13726
Values of x are: 10 2008 13726. For 10, s(x) is 1; Putting value of s(x) in equation b*(s(x)^a)+c we get 10, and as 10 lies in range 0<x<1e+9, therefore 10 is a possible answer, similar for 2008 and 13726. No other value of x satisfies the equation for the given value of a, b and c
Input: a = 2, b = 2, c = -1
Output: 1 31 337 967
Values of x that satisfy the equation are: 1 31 337 967
Approach: sumdigits(x) can be in the range of 1<=s(X)<=81 for the given range of x i.e 0<x<1e+9. This is because the value of x can be minimum 0 where sumdigits(x)=0 and maximum 999999999 where sumdigits(x) is 81. So first iterate through 1 to 81 to find x from the given equation, then cross-check if the sum of digits of the number found, is the same as the value of sum sumdigits(x). If both are the same then increase the counter and store the result in an array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getsum( int a)
{
int r = 0, sum = 0;
while (a > 0) {
r = a % 10;
sum = sum + r;
a = a / 10;
}
return sum;
}
void value( int a, int b, int c)
{
int co = 0, p = 0;
int no, r = 0, x = 0, q = 0, w = 0;
vector< int > v;
for ( int i = 1; i < 82; i++) {
no = pow (( double )i, double (a));
no = b * no + c;
if (no > 0 && no < 1000000000) {
x = getsum(no);
if (x == i) {
q++;
v.push_back(no);
w++;
}
}
}
for ( int i = 0; i < v.size(); i++) {
cout << v[i] << " " ;
}
}
int main()
{
int a = 2, b = 2, c = -1;
value(a, b, c);
return 0;
}
|
Java
import java.util.Vector;
class GFG
{
static int getsum( int a)
{
int r = 0 , sum = 0 ;
while (a > 0 )
{
r = a % 10 ;
sum = sum + r;
a = a / 10 ;
}
return sum;
}
static void value( int a, int b, int c)
{
int co = 0 , p = 0 ;
int no, r = 0 , x = 0 , q = 0 , w = 0 ;
Vector<Integer> v = new Vector<Integer>();
for ( int i = 1 ; i < 82 ; i++)
{
no = ( int ) Math.pow(i, a);
no = b * no + c;
if (no > 0 && no < 1000000000 )
{
x = getsum(no);
if (x == i)
{
q++;
v.add(no);
w++;
}
}
}
for ( int i = 0 ; i < v.size(); i++)
{
System.out.print(v.get(i)+ " " );
}
}
public static void main(String[] args)
{
int a = 2 , b = 2 , c = - 1 ;
value(a, b, c);
}
}
|
Python 3
def getsum(a):
r = 0
sum = 0
while (a > 0 ) :
r = a % 10
sum = sum + r
a = a / / 10
return sum
def value(a, b, c):
x = 0
q = 0
w = 0
v = []
for i in range ( 1 , 82 ) :
no = pow (i, a)
no = b * no + c
if (no > 0 and no < 1000000000 ) :
x = getsum(no)
if (x = = i) :
q + = 1
v.append(no)
w + = 1
for i in range ( len (v)) :
print (v[i], end = " " )
if __name__ = = "__main__" :
a = 2
b = 2
c = - 1
value(a, b, c)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int getsum( int a)
{
int r = 0, sum = 0;
while (a > 0)
{
r = a % 10;
sum = sum + r;
a = a / 10;
}
return sum;
}
static void value( int a, int b, int c)
{
int no, x = 0, q = 0, w = 0;
List< int > v = new List< int >();
for ( int i = 1; i < 82; i++)
{
no = ( int ) Math.Pow(i, a);
no = b * no + c;
if (no > 0 && no < 1000000000)
{
x = getsum(no);
if (x == i)
{
q++;
v.Add(no);
w++;
}
}
}
for ( int i = 0; i < v.Count; i++)
{
Console.Write(v[i]+ " " );
}
}
public static void Main(String[] args)
{
int a = 2, b = 2, c = -1;
value(a, b, c);
}
}
|
PHP
<?php
function getsum( $a )
{
$r = 0;
$sum = 0;
while ( $a > 0)
{
$r = $a % 10;
$sum = $sum + $r ;
$a = (int)( $a / 10);
}
return $sum ;
}
function value( $a , $b , $c )
{
$co = 0;
$p = 0;
$no ;
$r = 0;
$x = 0;
$q = 0;
$w = 0;
$v = array ();
$u = 0;
for ( $i = 1; $i < 82; $i ++)
{
$no = pow( $i , $a );
$no = $b * $no + $c ;
if ( $no > 0 && $no < 1000000000)
{
$x = getsum( $no );
if ( $x == $i )
{
$q ++;
$v [ $u ++] = $no ;
$w ++;
}
}
}
for ( $i = 0; $i < $u ; $i ++)
{
echo $v [ $i ] . " " ;
}
}
$a = 2;
$b = 2;
$c = -1;
value( $a , $b , $c );
?>
|
Javascript
<script>
function getsum(a)
{
let r = 0, sum = 0;
while (a > 0)
{
r = a % 10;
sum = sum + r;
a = Math.floor(a / 10);
}
return sum;
}
function value(a,b,c)
{
let co = 0, p = 0;
let no, r = 0, x = 0, q = 0, w = 0;
let v = [];
for (let i = 1; i < 82; i++)
{
no = Math.pow(i, a);
no = b * no + c;
if (no > 0 && no < 1000000000)
{
x = getsum(no);
if (x == i)
{
q++;
v.push(no);
w++;
}
}
}
for (let i = 0; i < v.length; i++)
{
document.write(v[i]+ " " );
}
}
let a = 2, b = 2, c = -1;
value(a, b, c);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...