Square formula.

Greetings, i am here in the search of help, i hope i find some.
So my problem is that i need to make a program that reads and interval between two given numbers from the user, and the program needs to give out the numbers which are a sum of two other squares ( x=a(2)+b(2) ). Can some one help me cause i can't imagine how to put this in code::block so that it works.
P.S the program needs to cout the numbers not the count of the numbers. (Just pointing out).
Last edited on
It looks like there are two parts to this question. The first is a fairly standard getting of two integers as input from the user. Lets call them from and to.
Example:
from = 20, to = 30
Then you would need a loop which iterates for each value in that range, in this case: 20, 21, 22, ... ... 29, 30

So far so good.
The next part is more interesting. There may be some nice mathematical theorem which covers this, but I don't think that is what is needed. Instead, I think a rather plain brute-force method could suffice.

Let's say we want to process one of the numbers in the range, say 25. Now you may already know that this can be made as the sum of 32 + 42
But we are not going to use any prior knowledge like that.
Instead, we take two numbers, call them a and b
Use a for loop which goes from a=1 to a= 24
Inside that loop, use a second for loop which goes from b=1 to b=24
inside that inner loop, use an if statement to test the condition (a*a + b*b) == n where n is our number, in this case 25.

If it is true, then we have successfully identified the value of n as being a required result, so it can now be printed out (and end both the loops).

I'd suggest all of that should be written as a separate function which takes the value n as input and returns a bool (true or false) result.

Last edited on
Topic archived. No new replies allowed.