jeudi 10 novembre 2016

How to correct my unit test for GCD function

I have written a simple GCD function to implement Euclid's algorithm for computing the greatest common divisor gcd(m, n), which is the largest integer k dividing both m and n.

The function that I wrote successfully compiles:

   public static int gcd(int m, int n) {
      if (n == 0) return m;
      return gcd(n, m%n);
   }

However, I run into an error when I write a unit test on GCD:

   @Test public void gcdTest() {
      for (int m = 0; m < 15; m++) {
      for (int n = 0; n < 15; n++) {
         assertEquals("Divide m,n", m/n%m, Recursion.gcd(m,n));
         }
      }
   }

The error comes in the 'assertEquals' line. I am unsure if maybe I am calculating this method incorrectly by writing m / n % m.

Any tips or suggestions? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire