3
\$\begingroup\$

I have solved this exercise, but it looks really redundant. Is there a built-in function in Java or a cleaner way to caluclate mod of a value + deviation without multiple OR statements? If I had to calculate this for a deviation of 5, this would then require a lot of repetition.

Given a non-negative number "num", return true if num is within 2 of a multiple of 10. Note: (a % b) is the remainder of dividing a by b, so (7 % 5) is 2.


    public boolean nearTen(int num) {
        return isMod10(num) || isMod10(num - 1) || isMod10(num + 1) || isMod10(num - 2) || isMod10(num + 2);
    }

    private boolean isMod10(int num) {
        return num % 10 == 0;
    }

\$\endgroup\$
5
\$\begingroup\$

You should read about congruent integers and modular arithmic. The distance of a number to a base is the minimum distance of its congruent value and its inverted congruent value.

 public boolean nearBase(int num, int base, int deviation) {

        // ..TODO check guards, normalize input or throw exceptions 
        // - base and deviation are expected strict positive integers
        // - deviation is expected smaller than base
        // - num is expected a positive integer

        var congruent = num % base;
        var inverted = base - congruent;
        var distance = Math.min(congruent, inverted);
        return distance <= deviation;
    }

 // your method rewritten ->
 public boolean nearTen(int num) {
        return nearBase(num, 10, 2);
    }
\$\endgroup\$
  • \$\begingroup\$ Okay, thanks! I had this topic in introductory algebra but did not think about it at all. \$\endgroup\$ – whatamidoingwithmylife Jun 1 at 15:54
2
\$\begingroup\$

Let's consider the results of numbers around 10 as an example :

7 % 10 = 7
8 % 10 = 8
9 % 10 = 9
10 % 10 = 0
11 % 10 = 1
12 % 10 = 2
13 % 10 = 3

We know that 8 to 12 should be included.

For 10 to 12 it's easy, we just need to check if their modulo is smaller than 2.

public boolean nearTen(int num) {
    int modulo = num % 10;
    return modulo <= 2;
}

For 8 and 9, if we look at the above results, we can see that the difference between their modulo and the original number will give us a number under 2. So we'll add that to the algorithm:

public boolean nearTen(int num) {
    int modulo = num % 10;
    return modulo <= 2 || num - modulo <= 2;
}

With that, we covered all cases. When facing a problem like this one, don't be afraid to write down the results and try to find a match.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.