bench-rejected-impls/13_shuttle_search.rb
# Modify the above to handle non-coprimes.
# I haven't worked out yet why this is correct,
# but the diff is pretty small.
# Just divide by the gcd at the appropriate time.
Modify the above to handle non-coprimes.
I haven't worked out yet why this is correct,
but the diff is pretty small.
Just divide by the gcd at the appropriate time.
Modify the above to handle non - coprimes . I haven ' t worked out yet why this is correct but the diff is pretty small . Just divide by the gcd at the appropriate time .
def depart_matching_offset_non_coprime(buses)
  step = 1
  t = 0
  buses.map { |busid, bus_offset|
    gcd, x = egcd(step, busid)
    initial_diff = -bus_offset - t
    raise "can't #{step} #{busid} because offset #{initial_diff} vs gcd #{gcd}" if initial_diff % gcd != 0
    t += step * (((initial_diff % busid) * x) % busid) / gcd
    step = step.lcm(busid)
    t
  }
end
def depart_matching_offset_non_coprime ( buses ) step = 1 t = 0 buses . map { | busid , bus_offset | gcd , x = egcd ( step , busid ) initial_diff = - bus_offset - t raise "can't #{step} #{busid} because offset #{initial_diff} vs gcd #{gcd}" if initial_diff % gcd != 0 t += step * ( ( ( initial_diff % busid ) * x ) % busid ) / gcd step = step . lcm ( busid ) t } end
Modify the above to handle non-coprimes.
Modify the above to handle non - coprimes .
buses
<not_specific>