Matthew_Thunder
Matthew_Thunder

Reputation: 39

For loop divide numbers

I'm an amateur when it comes to C++ but I've already received a task which is over my knowledge.

Task is to enter numbers n,m. Programme must take it as an interval, in which it checks if there is any number which is a sum of numbers with the same exponent.

EDIT:(18.10.15) Turns out I didn't understood my task correctly. Here it is:

"User enter two numbers. Programme takes it 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.

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

                for (int j=1; j<=5; j++)
                {
                    a = a * a;
                    b = b * b;
                    c = c * c;
                    d = d * d;
                    if (a + b + c + d == n)
                    {
                        cout << n << endl;
                    }
                }
        }
    }

Any help will be appreciated.

Upvotes: 3

Views: 5398

Answers (3)

Matthew_Thunder
Matthew_Thunder

Reputation: 39

Sorry that I answer so late and that I phrased my question poorly - English isn't my native language.

But turns out I didn't understood my task correctly. Here it is:

"User enter two numbers. Programme takes it 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.

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

                for (int j=1; j<=5; j++)
                {
                    a = a * a;
                    b = b * b;
                    c = c * c;
                    d = d * d;
                    if (a + b + c + d == n)
                    {
                        cout << n << endl;
                    }
                }
        }
    }

Upvotes: 0

juankirr
juankirr

Reputation: 323

"All I know is how to get the programm to check each number separately" "Programme must not use arrays."

    for (int i = n; i <= m; i++) {
        ...
        int x = (int)Math.log10(i);
        int rest = i;
        for (int p = x; p>=0; p--) {
            int digit = rest / (int)Math.pow(10,p);
            rest = i % (int)Math.pow(10,p);

            //3802 = 3*10^3 + 8*10^2 + 0*10^1 + 2*10^0 
        }
    }

    ...

Sorry, is Java no C++

Upvotes: 0

Shoe
Shoe

Reputation: 76240

Task is to enter numbers n,m. Programme must take it as an interval, in which it checks if there is any number which is a sum of numbers with the same exponent.

Assuming the range is given as [n, m), then here's your program:

return (n != m);

Any number can be seen as a sum of numbers with the same exponent. For example:

0 = 0 ^ 3 + 0 ^ 3 + 0 ^ 3
1 = 1 ^ 3 + 0 ^ 3
2 = 1 ^ 3 + 1 ^ 3
3 = 1 ^ 3 + 1 ^ 3 + 1 ^ 3

and so on. This is true even for negative numbers.

So in any non-empty range there exists at least 1 such number.

Upvotes: 1

Related Questions