An algorithm which calculates the minimal quantity of steps to walk from a point to another.

Knowing that

  • A is the starting point
  • B is the destination point
  • D is the step size

Assume that

  • A, B and D are integers and > 0;
  • A ≤ B.
public int calc(int A, int B, int D) {
    if ( A == B ) return 0;
    int dist = B - A;
    int steps = dist/D;
    if ( dist%D !=0 ){
        steps +=1;
    }
    return steps;
}

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s