Help with Mod operator {Equation}

Pages: 12
int x = whatever..
x = (x + 7) % 10

how can i do the opposite of the above?

What is the opposite of a mod operator?

This is a hw assignment that has to deal with encryption:

"Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10."

Is my equation from above right?

How can i decrypt this?
Last edited on
Can anybody help me please
The point of the homework is that you explore the mechanics of modulo. What would be the point if we just tell you the answer?
What is the opposite of a mod operator? considering mod is the remainder I would say the opposite would be the amount actually be divided.
helios there was a lot to this homework this is only a small part of it..I have completed the entire assignment up until this point..I do not know how to get back to the original number after applying this operation to the numbers.

giblit i need to get back the original numbers I have to do this equation in reverse.

x = (x - 7) ??% 10??
Last edited on
No way to do it unless you know x's mins and maxs which must be at most 7 numbers far.
Last edited on
5 % 3 = 2

8 % 3 = 2

@EssGeElch thanks for giving me an answer that was worth reading ...Yea there is no way.....I don't understand this homework question!!? What do they expect? Am I misinterpreting this line?

"Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. "
You need to play around with the operator. If you do that, you'll figure it out eventually.
Word of warning: you can't solve this problem by reducing the expression like you do with equations with elementary functions (i.e. x = y + 1 therefore y = x - 1). Modulo doesn't have an inverse operation because it's destructive. You have to use the properties of modulo:
* 0 <= n%m < m
* (n + a) % m == (n % m + a % m) % m
Corollary: (n + m) % m == n % m
Whoops, I meant at most 10 numbers far (0 to 9, a single digitt, exactly like the question says). This means it IS possible.
The basilar inverse equation is equivalent to:
x = (x+(10-7))%10
10 comes from the amount of numbers with a single digit (0 to 9).
7 comes from the number added previously.
Didn't test, but this should do the trick.
Play with the (10-7) value a bit and watch how the values slowly go off the track.
Last edited on
Well, just fucking great. Why do I even bother?
Well, just fucking great. Why do I even bother?
Because you care to help them understand how the solution works.

Anyways OP I'm not sure where you are getting reverse from
"Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. "
This sounds to me add 7 to the digit then get the remainder of that number or in other words digit = (digit + 7) mod 10 like you have in your first post

Maybe if you posted the actual assignment we would have a clearer idea on what you are supposed to be doing because I am a bit confused with what you are saying and the snippet

*EDIT I see what you are talking about. The assignment is to encrypt a text file by adding 7 to it and then getting mod 10.

Encryption List:
7
8
9
0
1
2
3
4
5
6

See any patterns?
Last edited on
Thanks for your help im at work right now..But the assignment is to take and encrypt a 4 digit number so like 1234 and then apply that quote from the text above to each number. so encypt it then do the reverse to decrypt
Last edited on
@EssGeEich

So close to the solution!!

num[i] = (num[i] + (10 - 7 ) % 10);

when i decrypt 1234 i get 11 12 3 4

This works for 1234
1
2
3
4
5
6
7
8
for(int i = 0; i < NUMBER_OF_DIGITS; i++)
		{ 
			num[i] = (num[i] + (10 - 7 ) % 10); // add mod
			if(num[i] > 10)
			{
				num[i] = (int) ((double)num[i] / 8 + 0.5);
			}
		}
Last edited on
I finally got it to FUC*&(**(&(*&ING WORK!!!


Thank you EssGeEich!!!!

doesn't work with zero tho...:(

GOT IT FINALLY!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void decrypt()
	{		
		numwitch(); // switch numbers back to original position
		int[] temp = new int[NUMBER_OF_DIGITS];
		
		for(int i = 0; i < NUMBER_OF_DIGITS; i++)
		{ 
			num[i] = (num[i] + (10 - 7 ) % 10); // add mod
			if(num[i] > 10)
			{
				num[i] = (int) ((double)num[i] / 8 + 0.5);
			}
			if(num[i] == 10)
			{
				num[i] = 0;
			}
		}
	}
Last edited on
That's not really the solution. You should end up with no ifs in the loop.
What is the order of
num[i] = (num[i] + (10 - 7) % 10); ?
What happens first and what happens last?
Last edited on
I had to add the if statements because the the 4 digit numbers have to be under 10.. so like

9 9 9 9...

I would get numbers over 9 after applying that equation you gave me, so...somehow dividing the numbers that were over 10 worked some numbers needed to a rounded up a bit after the division..if one of the numbers started out as a 0 it would become 10 during the encryption process...

And thats my reasoning behind those if statements.. And the program works great

If you are interested here is my final program it is written in Java.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//Frank Novello
//9/28/14
//Purpose: To encrypt/decrypt a 4 digit number and output to screen. 

import java.util.Scanner;

public class main {
	public static void main(String[] args) {
	
		int num = 0;
		
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a 4 digit number");
		
		while(num < 1000 || num > 9999 ){
			num = sc.nextInt();
			
			if(num < 1000)
			{
				System.out.println("Error: Number is less than 4 digits");
			}
			else if(num > 9999)
			{
				System.out.println("Error: Number is more than 4 digits");
			}	
		}
		Encryption ecrypt = new Encryption(num);
		ecrypt.print();
		ecrypt.decrypt();
		System.out.println();
		ecrypt.print();
	}
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

public class Encryption {
	private final int NUMBER_OF_DIGITS = 4;
	private int[] num = new int[NUMBER_OF_DIGITS];
	private int[] remainders = new int[NUMBER_OF_DIGITS];
	
	public Encryption(int numIn) 
	{
		//Separate 4 digit number into 4 separate Integers
		remainders[0] = numIn % 1000;
		num[0] = (numIn - remainders[0]) /1000;
		remainders[1] = remainders[0] % 100;
		num[1] = (remainders[0]-remainders[1])/100;
		remainders[2] = remainders[1] % 10;
		num[2] = (remainders[1]-remainders[2])/10;
		remainders[3] = remainders[2] % 1;
		num[3] = (remainders[2]-remainders[3])/1;
		
		//add 7 and take remainder after dividing by 10
		for(int i = 0; i < NUMBER_OF_DIGITS; i++)
		{
			num[i] = ((num[i] + 7) % 10); // add mod
		}
		
		numwitch(); // switch 1st number with 3rd and 2nd with 4th	
	}
	
	public void print()
	{
		for(int i = 0; i < NUMBER_OF_DIGITS; i++)
		{
			System.out.print(num[i] + " ");
		}
	}
	
	public void decrypt()
	{		
		numwitch(); // switch numbers back to original position
		int[] temp = new int[NUMBER_OF_DIGITS];
		
		for(int i = 0; i < NUMBER_OF_DIGITS; i++)
		{ 
			num[i] = (num[i] + (10 - 7 ) % 10); // add mod
			if(num[i] == 10)
			{
				num[i] = 0;
			}
			else if(num[i] > 10)
			{
				num[i] = (int) ((double)num[i] / 8 + 0.5); // trial and error to get correct values 
			}
		}
	}
	
	private void numwitch()
	{
		// switch 1st with 3rd and 2nd with 4th
		int temp;
		temp = num[0];
		num[0] = num[2];
		num[2] = temp;
		
		temp = num[1];
		num[1] = num[3];
		num[3] = temp;
	}
}
Last edited on
Still you must end up with no ifs.
If you cannot understand how to keep a number within 0-9 without ifs, look up what the modulo operator does, since it'll do everything for you.
Also there is a tiny important difference between my, uh, solution and yours.
I don't know man wasn't working for me..Run it then post your code if you want..
There is a tiny important difference between my, uh, solution and yours.
x= (x+(10-7))%10

damn...
Pages: 12