Code:
#include <iostream.h>
#include <stdlib.h>
int main()
{
int integer, integer_sqrt;
bool disabled;
std::cout << "Eneter an integer: ";
std::cin >> integer;
std::cout << "\n--------------------------------------------------------------------------------Possible addings:\n--------------------------------------------------------------------------------\n";
integer_sqrt = sqrt(integer);
int Sqrt_numbers[integer_sqrt];
for(int i=0; i<=integer_sqrt; i++)
{
Sqrt_numbers[i] = i*i; Sqrt_numbers[i] == !disabled;
}
for(int x=1; x<=sizeof Sqrt_numbers/4; x++)
{
for(int y=1; y<=sizeof Sqrt_numbers/4; y++)
{
if(Sqrt_numbers[x]+Sqrt_numbers[y] == integer)
{std::cout << Sqrt_numbers[x] << " + " << Sqrt_numbers[y] << "\n";}
for(int z=1; z<=sizeof Sqrt_numbers/4; z++)
{
if(Sqrt_numbers[x]+Sqrt_numbers[y]+Sqrt_numbers[z] == integer)
{std::cout << Sqrt_numbers[x] << " + " << Sqrt_numbers[y] << " + " << Sqrt_numbers[z] << "\n";}
for(int i=1; i<=sizeof Sqrt_numbers/4; i++)
{
if(Sqrt_numbers[x]+Sqrt_numbers[y]+Sqrt_numbers[z]+Sqrt_numbers[i] == integer)
{std::cout << Sqrt_numbers[x] << " + " << Sqrt_numbers[y] << " + " << Sqrt_numbers[z] << " + " << Sqrt_numbers[i] << "\n";}
}
}
}
}
system("PAUSE");
return 0;
}
Lagranges statement: if we disregard 1, all integers can be summarized by two, three or four
sqaure numbers. You will never have to use five or six.
Well, I decided to give this another try, now that I feel more experiened regarding C++.
There is now only one major issue left, which I have unsuccesfully tried to solve myself:
The program writes all of the possible summerizes for the integer, it's just that I sometimes
get the combinations more than one time. Example: type in "9" in the program, and the possible
summerizes of "1+4+4, 4+1+4, 4+4+1" will be displayed.
This is because the program adds the first number and on in the array, with the rest of the
numbers in the array. For example, the array looks like this: "1, 4, 9", then the program
will add "1+1, 1+4, 1+9, 4+1, 4+4, 4+9, 9+1, 9+4, 9+9". Each time the combination equals
the specific number, the combination is displayed. If the specific number is "5", it would
have been equaled by both 1+4 and 4+1, and thus displayed.
I have tried using a boolean value, to disable the specific combination. Like:
if(Sqrt_numbers[x]+Sqrt_numbers[y] == integer && Sqrt_numbers[x]+Sqrt_numbers[y] == !disabled)
{std::cout << Sqrt_numbers[x]+Sqrt_numbers[y]; Sqrt_numbers[x]+Sqrt_numbers[y] == disabled;}
In the loop where the array is initialized, I have also tried to give all of the numbers in it
the value of !disabled, as any number except 0 would get the value of true, and thus disabled.
Anyway, my purpose with this is that the current combination, for example: x=1 and y=4, are
together disabled, so that they won't be displayed again when x=4 and y=1. Unfortunately, this
doesn't work.
Any ideas?