📜  方程x = b *(sumofdigits(x)^ a)+ c的积分解数

📅  最后修改于: 2021-06-26 12:23:23             🧑  作者: Mango

给定a,b和c,它们是等式x = b *(sumdigits(x)^ a)+ c的一部分
其中sumdigits(x)确定数字x的所有数字的总和。任务是找出满足方程的x的所有整数解,并按升序打印。
鉴于此,1 <= x <= 10 9
例子:

方法:对于给定的x范围, sumdigits(x)可以在1 <= s(X)<= 81的范围内,即0 这是因为x的值在sumdigits(x) = 0时可以最小为0,在s umdigits(x)为81时可以最大为999999999。因此,首先迭代1到81从给定的方程中找到x,然后交叉检查是否找到的数字的位数之和与sumsumdigits(x)之和的值相同。如果两者相同,则增加计数器并将结果存储在数组中。
下面是上述方法的实现:

C++
// C++ program to find the numbers of
// values that satisfy the equation
#include 
using namespace std;
 
// This function returns the sum of
// the digits of a number
int getsum(int a)
{
    int r = 0, sum = 0;
    while (a > 0) {
        r = a % 10;
        sum = sum + r;
        a = a / 10;
    }
    return sum;
}
 
// This function creates
// the array of valid numbers
void value(int a, int b, int c)
{
    int co = 0, p = 0;
    int no, r = 0, x = 0, q = 0, w = 0;
    vector v;
 
    for (int i = 1; i < 82; i++) {
 
        // this computes s(x)^a
        no = pow((double)i, double(a));
 
        // this gives the result of equation
        no = b * no + c;
 
        if (no > 0 && no < 1000000000) {
            x = getsum(no);
 
            // checking if the sum same as i
            if (x == i) {
 
                // counter to keep track of numbers
                q++;
 
                // resultant array
                v.push_back(no);
                w++;
            }
        }
    }
 
    // prints the number
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int a = 2, b = 2, c = -1;
 
    // calculate which value
    // of x are possible
    value(a, b, c);
 
    return 0;
}


Java
// Java program to find the numbers of
// values that satisfy the equation
import java.util.Vector;
 
class GFG
{
 
// This function returns the sum of
// the digits of a number
static int getsum(int a)
{
    int r = 0, sum = 0;
    while (a > 0)
    {
        r = a % 10;
        sum = sum + r;
        a = a / 10;
    }
    return sum;
}
 
// This function creates
// the array of valid numbers
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 v = new Vector();
 
    for (int i = 1; i < 82; i++)
    {
 
        // this computes s(x)^a
        no = (int) Math.pow(i, a);
 
        // this gives the result of equation
        no = b * no + c;
 
        if (no > 0 && no < 1000000000)
        {
            x = getsum(no);
 
            // checking if the sum same as i
            if (x == i)
            {
 
                // counter to keep track of numbers
                q++;
 
                // resultant array
                v.add(no);
                w++;
            }
        }
    }
 
    // prints the number
    for (int i = 0; i < v.size(); i++)
    {
        System.out.print(v.get(i)+" ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int a = 2, b = 2, c = -1;
 
    // calculate which value
    // of x are possible
    value(a, b, c);
    }
}
 
// This code is contributed by
// PrinciRaj1992


Python 3
# Python 3 program to find the numbers
# of values that satisfy the equation
 
# This function returns the sum
# of the digits of a number
def getsum(a):
 
    r = 0
    sum = 0
    while (a > 0) :
        r = a % 10
        sum = sum + r
        a = a // 10
     
    return sum
 
# This function creates
# the array of valid numbers
def value(a, b, c):
 
    x = 0
    q = 0
    w = 0
    v = []
 
    for i in range(1, 82) :
 
        # this computes s(x)^a
        no = pow(i, a)
 
        # this gives the result
        # of equation
        no = b * no + c
 
        if (no > 0 and no < 1000000000) :
            x = getsum(no)
             
            # checking if the sum same as i
            if (x == i) :
 
                # counter to keep track
                # of numbers
                q += 1
 
                # resultant array
                v.append(no)
                w += 1
                 
    # prints the number
    for i in range(len(v)) :
        print(v[i], end = " ")
 
# Driver Code
if __name__ == "__main__":
     
    a = 2
    b = 2
    c = -1
 
    # calculate which value
    # of x are possible
    value(a, b, c)
 
# This code is contributed
# by ChitraNayal


C#
// C# program to find the numbers of
// values that satisfy the equation
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // This function returns the sum of
    // the digits of a number
    static int getsum(int a)
    {
        int r = 0, sum = 0;
        while (a > 0)
        {
            r = a % 10;
            sum = sum + r;
            a = a / 10;
        }
        return sum;
    }
 
    // This function creates
    // the array of valid numbers
    static void value(int a, int b, int c)
    {
        int no, x = 0, q = 0, w = 0;
        List v = new List();
 
        for (int i = 1; i < 82; i++)
        {
 
            // this computes s(x)^a
            no = (int) Math.Pow(i, a);
 
            // this gives the result of equation
            no = b * no + c;
 
            if (no > 0 && no < 1000000000)
            {
                x = getsum(no);
 
                // checking if the sum same as i
                if (x == i)
                {
 
                    // counter to keep track of numbers
                    q++;
 
                    // resultant array
                    v.Add(no);
                    w++;
                }
            }
        }
 
        // prints the number
        for (int i = 0; i < v.Count; i++)
        {
            Console.Write(v[i]+" ");
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int a = 2, b = 2, c = -1;
 
        // calculate which value
        // of x are possible
        value(a, b, c);
    }
}
 
// This code has been contributed by Rajput-Ji


PHP
 0)
    {
        $r = $a % 10;
        $sum = $sum + $r;
        $a = (int)($a / 10);
    }
    return $sum;
}
 
// This function creates
// the array of valid numbers
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++)
    {
 
        // this computes s(x)^a
        $no = pow($i, $a);
 
        // this gives the result
        // of equation
        $no = $b * $no + $c;
 
        if ($no > 0 && $no < 1000000000)
        {
            $x = getsum($no);
 
            // checking if the
            // sum same as i
            if ($x == $i)
            {
 
                // counter to keep
                // track of numbers
                $q++;
 
                // resultant array
                $v[$u++] = $no;
                $w++;
            }
        }
    }
 
    // prints the number
    for ($i = 0; $i < $u; $i++)
    {
        echo $v[$i] . " ";
    }
}
 
// Driver Code
$a = 2;
$b = 2;
$c = -1;
 
// calculate which value
// of x are possible
value($a, $b, $c);
 
// This code is contributed
// by mits
?>


Javascript


输出:
1 31 337 967

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。