31
4
I was at a friend's house for dinner and they suggested the idea of a "Prime-factor vector space". In this space the positive integers are expressed as a vector such that the nth element in the vector is the number of times the nth prime divides the number. (Note that this means that our vectors have an infinite number of terms.) For example 20 is
2 0 1 0 0 0 ...
Because its prime factorization is 2 * 2 * 5.
Since prime factorization is unique each number corresponds to one vector.
We can add vectors by pairwise adding their entries. This is the same as multiplying the numbers they are associated with. We can also do scalar multiplication, which is akin to raising the associated number to a power.
The problem is that this space is not in fact a vector space because there are no inverses. If we go ahead and add the inverses and close the vector space we now have a way of expressing every positive rational number as a vector. If we keep the fact that vector addition represents multiplication. Then the inverse of a natural number is its reciprocal.
For example the number 20 had the vector
2 0 1 0 0 0 ...
So the fraction 1/20 is its inverse
-2 0 -1 0 0 0 ...
If we wanted to find the vector associated with a fraction like 14/15 we would find 14
1 0 0 1 0 0 ...
and 1/15
0 -1 -1 0 0 0 ...
and multiply them by performing vector addition
1 -1 -1 1 0 0 ...
Now that we have a vector space we can modify it to form an inner product space by giving it an inner product. To do this we steal the inner product that vector spaces are given classically. The inner product of two vectors is defined as the sum of the pairwise multiplication of their terms. For example 20 · 14/15 would be calculated as follows
20 = 2 0 1 0 0 0 ...
14/15 = 1 -1 -1 1 0 0 ...
2 0 -1 0 0 0 ... -> 1
As another example the product 2/19 · 4/19
2/19 = 1 0 0 0 0 0 0 -1 0 0 0 ...
4/19 = 2 0 0 0 0 0 0 -1 0 0 0 ...
2 0 0 0 0 0 0 1 0 0 0 ... -> 3
Your task is to implement a program that performs this dot product. It should take two positive rational numbers via either a pair of positive integers (numerator and denominator) or a rational type (floats are not allowed, because they cause problems with precision and divisibility) and should output a integer representing the dot product of the two inputs.
This is code-golf so answers will be scored in bytes with fewer bytes being better.
Test Cases
4 · 4 = 4
8 · 8 = 9
10 · 10 = 2
12 · 12 = 5
4 · 1/4 = -4
20 · 14/15 = 1
2/19 · 4/19 = 3
A vector does not have a dimension, a vector space does. – Jonathan Frech – 2018-02-22T22:47:18.353
5@JonathanFrech I think it's a little bit pedantic, but I've made the change. – Post Rock Garf Hunter – 2018-02-22T22:49:55.483
"Natural numbers" is generally understood to contain 0, which isn't represented in your system. And these aren't vectors. A vector space is over a field, and this is over a ring, which would make this a module. And it isn't a separate space from the integers, it's the same space with a different representation. – Acccumulation – 2018-02-22T23:51:32.753
6@Acccumulation "Natural numbers" is not a well defined term, depending on who you ask it may or may not contain zero. You are correct that the "scalar multiplication" in my question forms a G-set with a monoid rather than a group, but that was simplified for the purposes of making the question palatable. I'm not sure what to make of your last comment, sure it has the same cardinality as the integers, but the action is really what defines a space not it's size. Perhaps you mean something more specific that I am missing. If so I'd be happy to continue this discussion (in chat might be best). – Post Rock Garf Hunter – 2018-02-23T00:24:08.347
Can we take input as
(num1, num2, den1, den2)
? – xnor – 2018-02-23T08:14:09.9732Another terminology nit-pick: Vector spaces are generally requred to have scalar multiplication from a field, so just using integers is not enough. That's because we want parallel vectors to be multiples of one another, not just have some multiple in common. For instance, $4$ and $8$ are parallel "vectors" in this space (they are both of the form (a, 0, 0,...)), but neither is a scalar multiple (i.e. an integer power) of the other. There aren't really many other terms you could use, though, that would be known to people in general. "Free module over the integers" is the best I can do. – Arthur – 2018-02-23T12:06:14.490
@xnor That's fine, I believe Luis Mendo has already done it. – Post Rock Garf Hunter – 2018-02-23T21:46:34.320