Problem 203. fly fly away

A fly moves following a predefined sequence of discrete jumps (defined by the vectors dx and dy) repeating the same sequence over and over (note that after each sequence of movements the fly may or may not be at the same position where it started).

The fly always starts at the coordinates (0,0) and your job is to determine whether it will ever reach a target coordinate ( x , y ).

note: dx and dy are always equal-sized row vectors and x and y are always scalars. Your function should return true if the trajectory passes through the target.


dx = [0,1,0,-1];
dy = [1,0,1,0];
x  = 1;
y  = 5;

The fly follows the trajectory:

 (0,0) (0,1) (1,1) (1,2) (0,2) (0,3) (1,3) (1,4) (0,4) (0,5) (1,5) (1,6) ...

The trajectory passes through the target point (1,5) so the function should return true.

note: in the test suite, if the target is reachable it will be reachable in at most 100 repetitions of the movement sequence. You get unscored bonus points if your solution does not require this limit to be relatively small (i.e. if the algorithm computational time does not increase linearly with the number of repetitions)

Solution Stats

60.84% Correct | 39.16% Incorrect
Last solution submitted on Sep 19, 2017

Problem Comments

Solution Comments


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!