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 comment