Need help for game software

magic

New member
Joined
Oct 4, 2011
Messages
9
Hello all

In part of the algo I'm writing for my game, I need x^2 + (a*b*y) to always be a square whole number. The only way I have of doing this is to check if it is a square, if not then get a new x^2 + (a*b*y). This takes up an awful lot of clock cycles. Is there a way to generate figures so that this will always be square?

TIA for any ideas
 
Hello, magic!

In part of the algo I'm writing for my game,
I need \(\displaystyle x^2 + aby\) to always be a square whole number.

Let .\(\displaystyle x^2 + aby \:=\:n^2\), for some integer \(\displaystyle n.\)

Then: .\(\displaystyle aby \:=\: n^2-x^2 \quad\Rightarrow\quad aby \:=\:(n+x)(n-x)\)


Assuming \(\displaystyle ab\,>\,y\)

. . let .\(\displaystyle \begin{Bmatrix}ab &=& n+x & [1] \\ y &=& n-x & [2]\end{Bmatrix}\)

Subtract [1] - [2]: .\(\displaystyle ab-y \:=\:2x \quad\Rightarrow\quad x \:=\:\dfrac{ab-y}{2}\)


Hope this helps . . .
 
Hello Soroban,

Thanks for that. It helped me along a good bit but I'm still stuck trying to get one variable in terms of the others. Is it ok to post what I have done?
 
If you're thinking "that's a silly reply...
then I'm thinking "that's a silly problem...;)

So....give us the "restrictions" on a,b,y; like 1 < a < b < y or something similar.

lol, point taken ;)

they're all whole numbers greater than zero and none of them are equal. The order of magnitude is unimportant
 
Don't you mean a,b,y are all > 1 ?

Yes, I did mean all > 1, sorry, didn't preview or check before posting

You seem to be looking to isolate each of x,a,b,y; here tizz:

sorry, no, that's not what I'm looking for because

x = SQRT(n^2 - aby)

this doesn't tell me if x is a whole number. Although it doen't sound like a big deal, to do this programatically, I would need to take the sqrt of (n^2 - aby), get the integer part of that, square it and compare to the original to see if it equaled (n^2 - aby), if not, go again. This takes a, relatively, long time. It would be much quicker if there was some way to 'know' that (n^2 - aby) was a square so that I would know that x was a whole number.

Here's one of the other formulas I have and what I done with it (not the same a and b btw)

a^2 + b^2 = 2c^2

[(a + b)/2]^2 + [(a - b)/2]^2 = c^2
let (a + b)/2 = u(u+2v)...(1)
and (a - b)/2 = 2v(u+v)...(2)
then I know that c is the whole number u^2 + 2uv + 2v^2 because (1)^2*(2)^2 = (u^2 + 2uv + 2v^2)^2
Adding (1) and (2) I get a = u^2 + 4uv + 2v^2
subtracting (1) - (2) I get b = u^2 - 2uv

so as long as u^2 > than 2uv, I know , for any whole numbers u and v, that a, b and c will also be whole numbers.

The answer that soroban gave is along the lines of what I'm looking for but there is a second formula that doesn't include a, b or c, that I can solve, but a third formula connects the other two.

Call the one above, the first one (it doesn't really matter what order they're taken in).

The second one is

d^2 - e^2 = 4f

and I done this

(d + e)(d - e) = 4f
let d + e = p
and d - e = q
then
d = (p+q)/2
e = (p-q)/2
f = pq/4

so the same as the first, for any whole p and q, I know that 2d, 2e and 4f will be whole numbers.

The third formula is

g^2 = e^2 + a^2 - c^2

so this has a and c in common with the first and e in common with the second. I was hoping to solve this and be able to p and q in terms of u and v to connect all three

putting in the values I've already "solved"

g^2 =((p-q)/2)^2 + 4uv(u+v)(u+2v)........................<------------- where my op came from

Soroban's answer was similar to what I'd already done for the second

(g + (p-q)/2)(g - (p-q)/2) = 4uv(u+v)(u+2v)
let g + (p-q)/2 = 4uv(u+v)
and g - (p-q)/2 = (u+2v)
then
g = (4uv(u+v) + (u+2v)) / 2
(p-q)/2 = (4uv(u+v) - (u+2v))/2

this gives me p - q = 4uv(u+v) - (u+2v)
so p = 4uv(u+v) - (u+2v) + q

for d, e and f I now have

d = (4uv(u+v) - (u+2v) + 2q)/2
e = (4uv(u+v) - (u+2v) )/2
f = (4uv(u+v) - (u+2v) + q)q/4

I can't find a way to get q in terms u and v to eliminate it from the values for d and f
 
blast!

left the first formula like I'd solved it

a = u^2 + 4uv + 2v^2
b = u^2 - 2v^2
c = u^2 + 2uv + 2v^2

solved g^2-e^2 = a^2 - c^2 a la soroban to get

e = [4uv(u+v)+u+2v]/2
g = [4uv(u+v)-u-2v]/2

and the third one

d^2 - e^2 = 4f
4d^2 - 16f = (4uv(u+v)+u+2v)^2
noticed that (4uv(u+v)+u+2v)^2 = (u+2v)^2 -8uv(u+v)(u+2v-2u^2v-2uv^2)
let 4d^2 = (u+2v)^2
and 16f = 8uv(u+v)(u+2v-2u^2v-2uv^2)

d = (u+2v)/2
f = [uv(u+v)(u+2v-2u^2v-2uv^2)]/2

bingo! nope

f will be negative unless u and v are fractions and that would muck up the rest of them :(

any help out there guys ¿
 
Solved :p

a = 16(u^2+4uv+2v^2)

b = 16(u^2-2v^2)

c = 16(u^2+2uv+2v^2 )

d = 32uv(u+v)

e = 8(4uv(u+v)-u-2v)

f = 8uv(u^2+3uv+2v^2 )-(u+2v)^2

g = 8(4uv(u+v)+u+2v)
 
Top