Problem with homtask

Write your question here.
Hello, I'm an amateur to C++ but I'm learning. And also having a problem with my second hometask. Here it is.

"User enters two numbers (n,m). Programme takes them as the interval in which it checks all the numbers. If there's a number in interval which all digit's sum of SAME exponent is that number, then programme shows it."

For example, I enter 100 and 200. In this interval there's 153.

153 = 1^3 + 5^3 + 3^3 (1+125+27)

Programme shows 153.

And so on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    int n,m;
    double x = 1;
    int f;
    int a,b,c,d;
    cout << "Please, enter two numbers" << endl;
    cin >> n;
    cin >> m;

        for (int i=n; i<=m; i++)
        {
                    a = i % 10; //for example, I enter 153, then a=3
                    f = i /= 10; //f=15
                    b = f % 10;  //b=5
                    f = f /= 10; //f=1
                    c = f % 10; //c=1
                    f = f /= 10;
                    d = f % 10; //d=0

                    for (int j=0; j<=4; j++) //loop to determine exponent
                    {
                        a = pow(a,x);
                        b = pow(b,x);
                        c = pow(c,x);
                        d = pow(d,x);
                        x = x + 1;
                        if (a+b+c+d == n)
                        {
                            cout << n << endl;
                        }


I made 4 integers (a,b,c,d) in case user inputs number with 4 digits. Don't know other approach than to seperate number into digits. But programme doesn't react, only if I enter 1 to 100, it it looping just 1 all the time.
What am I doing wrong?

Any help will be appreciated.
Brute force: (without using functions since it may not have been covered as yet):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

int main()
{
    std::cout << "Please, enter two numbers\n" ;
    int n,m;
    std::cin >> n >> m ;
    
    std::cout << "interval [" << n << ',' << m << "]\n-----------------------\n" ;

    // for each number in the inclusive interval [n,m]
    for( int number = n ; number <= m ; ++number )
    {
        // find the count of digits in number
        int digit_count = 0 ;
        {
            int temp = number ;
            while( temp != 0 ) // repeat as long as there are some digits left
            {
                ++digit_count ;
                temp /= 10 ; // divide by 10 to reduce temp to the remaining digits
            }
        }

        // find the sum of powers of the digits of number
        int sum = 0 ;
        {
            int temp = number ;
            while( temp != 0 ) // as long as there are some digits left
            {
                const int digit = temp % 10 ; // get the least significant decimal digit

                // compute its power to digit_count
                int pow = 1 ;
                for( int i = 0 ; i < digit_count ; ++i ) pow *= digit ;

                sum += pow ;
                temp /= 10 ; // divide by 10 to reduce temp to the remaining digits
            }
        }

        if( sum == number ) std::cout << number << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/6e75c07ee1b1dede
Last edited on
Thank You, JLBorges!
It's brutal approach, but it works like a charm! And You understood it correctly - I can't yet use functions. So Thank You a lot, You are a master!

It's just when I'm trying to delve into the code, I can't seem to understand the second part of it.

1
2
3
4
5
6
7
8
const int digit = temp % 10 ; // for example I take 153,
                                            //then 1st time digit here is 3.
                int pow = 1 ; //it looks constant to me
                for( int i = 0 ; i < digit_count ; ++i ) pow *= digit ; // looping 3 times,
                //cause number consists of 3 digits. 1st time: 1*3, 2nd time: 1*5, 3rd time 1*1

                sum += pow ; //adds to sum value of pow. 1st time: 3 | 2nd time: 5 | 3rd time: 1. 
                 //So sum becomes 9, but it's not equal to 153.  


What am I not getting here? Just have to understand it, because I must presentate it.


And if I'd like to also add digits and size of power to output (so it doesn't show only 153, but whole 153=1^3+5^3+3^3), is it even possible with minor changes in this souce code?
Last edited on
> I can't seem to understand the second part of it.

When you can't understand some part of the code, or you want to discover and correct some error in it, put in some debugging output statements which can show you what is going on.

As a personal choice, we tend not yo use debuggers beyond getting a stack trace or the value of a variable or two. ... we find stepping through a program less productive than thinking harder and adding output statements and self-checking code at critical places.
...
Blind probing with a debugger is not likely to be productive.

Brian Kernighan and Rob Pike in 'The Practice of Programming'


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

int main()
{
    int number = 153 ;
    int digit_count = 3 ;

    // find the sum of powers of the digits of number
    int sum = 0 ;
    {
        int temp = number ; // temp == 0
        while( temp != 0 ) // as long as there are some digits left
        {
            std::cout << "temp == " << temp << '\n' ;

            const int digit = temp % 10 ; // get the least significant decimal digit
            std::cout << "\tdigit == " << temp << "%10 == " << digit << '\n' ;

            // compute its power to digit_count
            int pow = 1 ;
            std::cout << "\t\tpow == " << pow << '\n' ;
            for( int i = 0 ; i < digit_count ; ++i )
            {
                std::cout << "\t\t\ti == " << i << " pow == " << pow << '*' << digit << " == " ;
                pow *= digit ;
                std::cout << pow << '\n' ;
            }

            std::cout << "\tsum == " << sum << '+' << pow << " == " ;
            sum += pow ;
            std::cout << sum << '\n' ;

            std::cout << "temp == " << temp << "/10  == " ;
            temp /= 10 ; // divide by 10 to reduce temp to the remaining digits
            std::cout << temp << "\n----- end this iteration -----\n\n" ;
            if( temp == 0 ) std::cout << "*** temp == 0, We are done ****\n\n" ;            
        }
    }

    std::cout << "number == " << number << ", sum == " << sum << '\n' ;
}

temp == 153
	digit == 153%10 == 3
		pow == 1
			i == 0 pow == 1*3 == 3
			i == 1 pow == 3*3 == 9
			i == 2 pow == 9*3 == 27
	sum == 0+27 == 27
temp == 153/10  == 15
----- end this iteration -----

temp == 15
	digit == 15%10 == 5
		pow == 1
			i == 0 pow == 1*5 == 5
			i == 1 pow == 5*5 == 25
			i == 2 pow == 25*5 == 125
	sum == 27+125 == 152
temp == 15/10  == 1
----- end this iteration -----

temp == 1
	digit == 1%10 == 1
		pow == 1
			i == 0 pow == 1*1 == 1
			i == 1 pow == 1*1 == 1
			i == 2 pow == 1*1 == 1
	sum == 152+1 == 153
temp == 1/10  == 0
----- end this iteration -----

*** temp == 0, We are done ****

number == 153, sum == 153

http://coliru.stacked-crooked.com/a/2694935550fecc58
Tahnk You, JLBorges.

After diving into the code, I understood every part of it. Big THANKS!
Hei,

I'm back with the same question. Turns out that JLBorges offered code isn't suitable, because in his code exponent IS STATIC with the count of digits in number.

Exponent MUST differ. It's static for all the digits. But it can be bigger or smaller than the count of digits in number.

Sorry for my example with number 153 as it has lead to misunderstood.

But programm should check if 1^2 + 5^2 + 3^2 is 153. Exponent, which in this case is 2, could be any number. Even bigger than 3. But in this example it's enough with 3, because sum of digits is the number.

I'm trying to play with the code but I'm learning so I haven't made any useful code. I was thinking of defining every digit with its own variable, like in my first post, but it's still not working. What should I do? Can someone suggest something?

P.S. I understand that I must be aware of number 1. Because number 1 can have exponent with any size and every number with exponent 0 is 1.
Jeez, am I only one thinking that this is quite hard for an amatuer in C++?
Last edited on
> Exponent MUST differ. It's static for all the digits. But it can be bigger or smaller than the count of digits in number.

So get rid of the loop that counts the number of digits; it is no longer required.
Instead put the computation of the sum of powers of the digits in a loop where the exponent goes from zero upwards.


Q: When would this loop end?

a. when the sum exceeds the value of the number (bigger exponents would give even larger numbers)

> I understand that I must be aware of number 1.
> Because number 1 can have exponent with any size and every number with exponent 0 is 1.

or b. when the number consists of only zeroes or ones. One way to check for this is to keep track of the largest digit in the number. Another is to check if the sum of powers of the digits remains unchanged even when the exponent is bigger.

Spoiler: http://coliru.stacked-crooked.com/a/c0470429e4126e88
I just wanted to notify You, JLBorges, that the second time I passed this hometask.

Thank You. Withoy You I don't know how I would've managed it. It would have taken from me a lot longer time.

Much aprreciated.
Topic archived. No new replies allowed.