Number of integral solutions for equation x = b*(sumofdigits(x) ^ a)+c


Suppose you are given three integral numbers a, b and c and you have an equation x = b* (sumofdigits(x)^a) +c. Here, sumofdigits(x) is the total sum of all the digits in x. To find all the possible integral solutions which satisfy the equation, we will explore various approaches in C++.

Input Output Scenarios

Given below are values of a, b and c. Different integral solution which satisfies the equation x = b* (sumofdigits(x)^a) +c is given as output.

Input: a = 2, b = 2, c = -3
Output: 125, 447, 575

In the above scenario the value a = 2, b = 2, c = -3 and the possible values of x are 125, 447 and, 575.

Consider 125, the sum(x) (i.e. the sum of digits in it) is 8 and, if you put this value in the equation b*(sum(x)^a) +c, the answer comes to 125 which is equal to x. Hence, it is possible solution of the equation.

Note − The integral solutions of this equation lie within the range of 1 to 109.

Using Recursion

We can use recursive search to find the integral solutions of the given equation.

We need to create a function named sumOfDigits() which is used to calculate the sum of the digits of any given number N.

  • Iterate the digits of N using modulus operator and division operator.

  • The modulus operator is used to extract the last digit of N.

  • After each iteration, add the digits one by one which is stored in the variable sum.

We create an integralSolutions() function which calculates the integral solutions.

  • It calculates the sum of digits of x using the sumOfDigits function.

  • Next, using the for loop we raise the sum to the power of a.

  • We evaluate the right-hand side of the equation by multiplying b to the power and adding c to it.

  • If the value of x is equal to right-hand side value, then it is considered as an integral solution.

Next, we have recursion function which searches those integral solutions which are within the specified range.

Example

#include <iostream>
using namespace std;

int sumOfDigits(int N) {
   int sum = 0;
   while (N != 0) {
      sum += N % 10; // addition of the last digit of N
      N /= 10;
   }
   return sum;
}
void integralSolutions(int x, int a, int b, int c) {
   int sum = sumOfDigits(x);
   int power = 1;
   for (int j = 0; j < a; j++) {
      power *= sum;
   }
   int rightHandSide = b * power + c;
   if (x == rightHandSide) {
      std::cout << "Integral solution: " << x << std::endl;
   }
}
void recursion(int start, int end, int a, int b, int c) {
   if (start > end) {
      return;
   }
   integralSolutions(start, a, b, c);
   recursion(start + 1, end, a, b, c);
}
int main() {
   int a = 1, b = 3, c = 5;
   recursion(1, 100000, a, b, c);
   return 0;
}

Output

Integral solution: 11
Integral solution: 38

Segmentation fault Error This error occurs when the end value of the specified range in the recursive search exceeds 100000. So, you cannot have values of x more than that value.

Using Simple Iteration

If you want integral solutions of x which are more than 100000, then we do not use recursion. Here, we will use the simple iteration of values of x from 1 to 109 and compare it with the right-hand side value of the equation.

Example

#include <iostream>
using namespace std;

int sumOfDigits(int N) {
   int sum = 0;
   while (N != 0) {
      sum += N % 10;
      N /= 10;
   }
   return sum;
}

bool integralSolution(int x, int a, int b, int c) {
   int sum = sumOfDigits(x);
   int power = 1;
   for (int i = 0; i < a; i++) {
      power *= sum;
   }
   int rightHandSide = b * power + c;
   return x == rightHandSide;
}

int main() {
   int a = 3, b = 5, c = 8;
   // x ranges from 1 to 109
   for (int x = 1; x <= 1000000000; x++) {
      if (integralSolution(x, a, b, c)) {
         std::cout << "Integral solution: " << x << std::endl;
      }
   }
   return 0;
}

Output

Integral solution: 53248
Integral solution: 148963

Conclusion

We have explored methods to find the integral solutions of the equation x = b* (sumofdigits(x)^a) +c which includes using recursion or simple iteration. The recursive approach allows you to flexibly specify the range of the solution. However, it has increased time complexity and may show segmentation error for larger range of values causing stack overflow.

The iterative approach is efficient for both time complexity and memory usage. However, it provides limited flexibility and has more complex codes. So, both the approaches have its own benefits and shortcomings. You can choose any approach according to your requirements.

Updated on: 12-Jul-2023

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements