Find if an integer is the sum of 2 powers

Hello,im trying to make a program that when you give it a number x it will output how many ways there are possible to get x by the sum of 2 powers(only powers that their base is raised by 2),if there are any.I will give an example so you can understand:
 
Input:50

1
2
Output:2
(there are 2 sums of powers that are equal to 50(i think so)(7^2+1^2 and 5^2+5^2))

Sorry for bad english and thanks in advance.
Last edited on
¿so what do you need help with?
This was fun to do. This is a starting point. You will still need to find out how to get the amount of outputs first before showing the outputs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    unsigned int numInput = 0, highestPossibleBase = 0;
    char** outputs;

    cout << "Input a number: ";
    cin >> numInput;
    cout << "-----\n";

    highestPossibleBase = sqrt(numInput);

    for(unsigned int i = 1; i <= highestPossibleBase; i++)
        for(unsigned int j = 1; j <= highestPossibleBase; j++)
            if(pow(i, 2) + pow(j, 2) == numInput)
                cout << i << "^2 + " << j << "^2 = " << numInput << endl;

    return 0;
}
Last edited on
@PopEye thanks for the help :),but as i saw it doesnt work with some numbers like 0 or 1 because 0 = 0^2 + 0^2 and 1 = 0^2 + 1^2.
I tried using a loop to check if any sums of all possible numbers raised by 2 are equal to 50, then add +1 to int ways.I dont know where the mistake is but here is my code:
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
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    int x = 0, y = 0 , ways = 0 , a;
    cout  << "Enter Number:";
    cin >> a;
    int pow1 = pow(x,2) + pow(y,2);
    for ( int i = 0 ; i < 11 ; i++ ){
        if(pow1 != a){
           int y = y + 1;
            if (y == 10){
              int y = y - 10;
              int x = x + 1;
            }
        }
        else if(pow1 == a){
           int ways = ways + 1;
        }
    }
    if(ways >= 1){
       cout << "/n Number Of Ways:" << ways << endl;
    }
    else{
       cout << "No Ways Available!" << endl;
    }
    return 0;
}

This would check if any of the 110,possible sums of 2 powers raised by 2,between 0^2 +0^2 and 10^2 + 10^2,and then add +1 to ways if pow1 is equal to the input but it doesnt.Also this would only work for numbers smaller than 200(as 10^2+10^2=200).A bit complicated,isnt it?
Last edited on
but as i saw it doesnt work with some numbers like 0 or 1 because 0 = 0^2 + 0^2 and 1 = 0^2 + 1^2.


In the two for loops, when I initialized i and j, their set to 1. Simply change the 1 to a 0 like so:

1
2
for(unsigned int i = 0; i <= highestPossibleBase; i++)
    for(unsigned int j = 0; j <= highestPossibleBase; j++)


That should work.
highestPossibleBase = sqrt(input), 'cuz input = highestPossibleBase2 + 02. If it's floating point value just floor() it.
Last edited on
Topic archived. No new replies allowed.