Home > Software design >  implementing mod 11 function in Java
implementing mod 11 function in Java

Time:10-29

good day, what is the best way to implement the below function in java: mod11(x) which calculates x mod 11, we assume x is either an integer or a formula. For example, mod11(14) returns 3, and mod11(3*7-30) returns 2. I tried the below but it didn't work: `

public static int PositiveMod(int value, int mod)
    {
        return ((value % mod   mod) % mod);
    }

`

`

public static double PositiveMod(double value, double mod)
    {
        return ((value % mod   mod) % mod);
    }

` for example, i expect ((-7 1)/20) mod 11 to give out 3 but instead it gave me 10.7 like below i = ((-7 1)/20) mod 11 = -6/9 mod 11 = 5*5 mod 11 = 3

CodePudding user response:

Your question is confusing.. the % operator is already the modulus operator, also known as remainder. ... so 14 % 11 = 3 is the same as mod11(14) = 3. There is no need to implement it as a new function.

In your equation, ((-7 1)/20) = -0.3, Its not clear why you would expect 3.

11 - 0.3 is 10.7, so that is where that answer comes from.

CodePudding user response:

% is module operation. So this is example how to use it:

public class MainClass2 {

    public static void main(String[] args) {
        System.out.println(modCalc(14, 11));
        System.out.println(modCalc((3*7-30), 11));
    }

    private static int modCalc(int a, int m)
    {
        return a % m;
    }
}

it returns: 3, -9

CodePudding user response:

% is not a mod function. It is a remainder function and behaves as one would expect a remainder to behave.

System.out.println(-10 % 3); //  -3 r -1 since 3 * -3   -1 == -10
System.out.println(10 % -3); //  -3 r  1 since -3 * -3   1 == 10 
System.out.println(-10 % -3);//   3 r -1 since 3 * -3   -1 == -10
System.out.println(10 % 3);  //   3 r 1 since 3 * 3   1 == 10

prints as expected
-1
1
-1
1

A true mod function for n = x mod(m) says there is some k where x - n = km

n = 20 mod(3) = 2  and k = 6   20 - 2 = 6*3
n = 20 mod(3) = -1 and k = 7   20 -(-1) = 3*7

The complete set of residues for any mod function is infinite and for the above is n = x - mk for any integral value of k

So for the above the complete residue set would be n = 20 - 3k. Any value returned would be a legitimate result. The remainder function is simply a subset of size one of the aforementioned set of residues.

Have said that, this may help. The mod function simply corrects to the next positive value.

Mod mod11 = Mod.forMod(11);
int k = mod11.of(-3);
System.out.println(k);

prints

8

Here is a way to create mod function that just takes the target argument.

 interface Mod {
     int of(int value);
     static Mod forMod(int mod) {
         return v-> {
             int u = v % mod;
             if (u < 0) {
                 u  = mod;
             }
             return u;
         };
     }
 }

The above may need some tweaking either because I misunderstood your issue or because the mod function needs to be complex to suit your requirements.

  •  Tags:  
  • java
  • Related