Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove a modulo out of my math operation?

Tags:

java

modulo

My prof does not like the use of modulo since it's not efficient enough, but I'm not sure how else I can get the same answer using a logic operator or something. Can someone help me out with how I can do this?

j = (j + 1) % a.length;
like image 744
user1730056 Avatar asked Oct 15 '13 19:10

user1730056


People also ask

How do you get rid of modulus in math?

If you square both sides of each equation then you can omit the modulus signs (as (x+3)^2 and (1-x)^2 will both be positive, regardless of whether x is a positive or negative number.)

How do you reverse a MOD function?

. The rule says to multiply the input by 3 and then add 1. Intuitively, the rule for the inverse should just reverse this process: subtract 1, then divide by 3. In fact, mod 7 we can divide by 3 by just multiplying by 3's multiplicative inverse (which is 5), so this rule makes sense modulo 7 as well.


1 Answers

This should do the trick.

 int k = a.length;
 int d = (j+1)/k;
 j = (j+1) - d*k
like image 57
Ankit Rustagi Avatar answered Sep 18 '22 10:09

Ankit Rustagi