Generate 3d Vectors with Integer Norms

1.1k Views Asked by At

I've written a program that generates pairs of integer 3d vectors and finds the angle between them.

I save the answer (the angle) as a rational multiple of pi:

    m = newRandomVector(3);
n = newRandomVector(3);
str_m = vector_to_string(m);
str_n = vector_to_string(n);
double mag1 = m.norm();
double mag2 = n.norm();
double prod = mag1 * mag2;
double cos = m.dot(n) / (mag1 * mag2);
double angle = acos (cos) * 180.0; /// PI;
sprintf(answer, "%lf/%lf * PI ", m.dot(n), prod);

However, generating random vectors crates almost exclusively float magnitudes.

How can I generate a 3D vector with an integer magnitude?

1

There are 1 best solutions below

1
On

You seem to have two concerns. One is immediate, all solutions to $a^2 + b^2 + c^2 = d^2 $ with $\gcd(a,b,c,d) = 1$ are given by a formula probably known to Euler, but associated with V. A. Lebesgue. It is not possible to have $d$ even unless the others are also even, making the gcd 2 or larger. We take $a,d$ as the odd numbers, with $$ a = m^2 + n^2 - p^2 - q^2, $$ $$ b = 2 (mq+np), $$ $$ c = 2 (nq - mp), $$ $$ d = m^2 + n^2 + p^2 + q^2. $$

I can add that we can find all solutions to $a^2 + b^2 + c^2 = 3 d^2.$ With two such vectors, the ratio involved in the cosine of the angle between them would be rational (not the angle over pi, that is quite rare). See Niven's book, Irrational Numbers.