Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse of modulo operator in Python

Tags:

python

math

I’m trying to solve the following set of equations for time in Python:

position1 = (initial_position1 + (time * velocity1)) % 360
position2 = (initial_position2 + (time * velocity2)) % 360

There are two objects revolving around a center point in the same direction, with different speed, and with possibly different starting points (ie. their position at a given time). I’d like to know how much time does it take for the two objects to meet.

For this, I have this equation:

(initial_position1 + (time * velocity1)) % 360 == (initial_position2 + (time * velocity2)) % 360

But I cannot solve it for time as there is no inverse function for the % operator. Is there a known solution for this? If not, any advice would come in handy.

like image 277
GergelyPolonkai Avatar asked Apr 11 '26 00:04

GergelyPolonkai


1 Answers

you're trying to solve

x1 + t * v1 = x2 + t * v2
and 
x1 - x2 = 360 * n

this translates into

n = t / 360 * (v2 - v1)
or
t = n * 360 / (v2 - v1)

you know what v2 and v1 are so this is just a linear equation with integer solutions. Find all points on that line where n is an integer and take smallest positive one.

like image 183
piRSquared Avatar answered Apr 12 '26 13:04

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!