-1

i got this question and i dont understand what this program is supposed to do.

the question is:

Show the pairs of numbers between 1 and 1000 whose sum of squares is a power third of any number, and the sum of their third powers is the square of some number. Do not use mathematical functions such as ..., sqrt, pow

Can anyone explain what I'm supposed to do, and how exactly can a loop replace the pow and sqrt functions.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • 3
    *Do not use mathematical functions such as ..., sqrt, pow* -- The `sqrt` and `pow` functions are floating point functions. This is good advice, given that the program you are writing is integer-based. – PaulMcKenzie Nov 16 '22 at 19:17
  • 2
    I'd very strongly you start by translating "human language" to math notation. That alone might help you, because it might show you a way how you could iterate through all possible things. After that it becomes much easier – translating math to code is not as hard as modelling the mathematical problem, usually. – Marcus Müller Nov 16 '22 at 19:18
  • So long as we're operating in integers, `pow` as a loop is relatively simple. 2 to the X is simply 2 times itself X-1 times. That's an easy loop. `sqrt` is trickier, but often you can avoid it entirely because for purposes of comparison of X and sqrt(Y) instead of taking the square root of Y you can square X. – user4581301 Nov 16 '22 at 19:20
  • Hint: 1) a *square* of a number: `(x * x)`. 2) Sum of squares: `(x * x) + (y * y) + (z * z)`. – Thomas Matthews Nov 16 '22 at 19:21
  • 1
    @OP You may wonder why floating point functions shouldn't be used in programs that deal with integers. [See this link as to floating point math surprises](https://stackoverflow.com/questions/588004/is-floating-point-math-broken), and see [this link](https://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os) as to why functions like `pow` may not work the way you think it does. – PaulMcKenzie Nov 16 '22 at 19:28
  • `pow(5,2)` may return 24 instead of 25 or other similar errors caused by the truncation of a floating point number to an int. – drescherjm Nov 16 '22 at 19:36
  • @MarcusMüller this is my problem, I'm struggling with Turing human language to math notation not with the code itself, what does the question mean with the sum of squares is a power third of any number? – Milad Bannourah Nov 16 '22 at 19:39
  • I assume the person stating the question is not a native English speaker (neither am I!) and what they meant was "when you take the square of both numbers, add these, you get a new number. Check whether that number is the third power of some arbitrary integer." – Marcus Müller Nov 16 '22 at 19:41

1 Answers1

0

Show the pairs of numbers between 1 and 1000 whose sum of squares is a power third of any number, and the sum of their third powers is the square of some number. Do not use mathematical functions such as ..., sqrt, pow

Let's try it.

Show the pairs of numbers between 1 and 1000

for(int i = 0; i <= 1000; i++)
for(int j = i; j <= 1000; j++) // starting at i to not repeat pairs twice

whose sum of squares

for(int i = 0; i <= 1000; i++)
for(int j = i; j <= 1000; j++) // starting at i to not repeat pairs twice
{
     int sumSquare = i * i + j * j;

is a power third of any number

for(int i = 0; i <= 1000; i++)
for(int j = i; j <= 1000; j++) // starting at i to not repeat pairs twice
{
     int sumSquare = i * i + j * j;
     bool found = false;
     for(int k = 1; k * k * k <= sumSquare; k++) // inefficient
     {
         if (k * k * k == sumSquare)
         {
             found = true;
         }
     }

     if (found)
     {
          cout << i << " " << j << " " << k << endl;
     }
}

and so on... You'll need to add the third condition, you may need to deal with overflows, maybe use size_t, and make it more efficient.

But that should give you the correct ideas...

Jeffrey
  • 11,063
  • 1
  • 21
  • 42