- 42,978
- 10,571
SammyS said:Let's look at the initial equation again.
We have (y − 97 + i) mod 26 = x − 97
For encrypting, you start with a y value such that 97 ≤ y ≤ 122 .
This gives 0 ≤ (y − 97) ≤ 25 .
To this we add i, then perform the mod 26 operation so that values of x fall into the same range of values as do values of y.
Specifically, 0 ≤ (x − 97) ≤ 25 and thus 97 ≤ x ≤ 122 .
So you could say that it's that i value which which makes inverting this expression a bit tricky.
The quantity, (y − 97 + i), falls in the range of values, [ i , 25 + i ] .
The mod 26 operation preserves any of these values falling in the [ 0 , 25 ] interval. Any other values are put into the [ 0 , 25 ] interval, by adding or subtracting appropriate multiples of 26.
Consider the following idea:
If you were to subtract i from (y − 97 + i), then of course you have (y − 97)
Similarly, if you subtract i from (x − 97). that gives (x − 97 − i). The values for this fall in the interval [ −i , 25 − i ]. Applying the mod 26 operation to this should give the result you need to obtain (y − 97) .
.
If the % operator does indeed give the remainder,then that should give correct results.Vital said:Unfortunately, no. If I understood you correctly, then the expression should be:
(x – 97 – i)%26 + 97 = y
But this is the expression I came up with a week ago, when I tried to solve the problem. It doesn't work, and produces an inappropriate result.
Use: y = (x – 97 – i)%26 + 97Vital said:Let's say I have 3 initial equations:
(1) (99 – 97 + 17) mod 26 + 97 = x; x = 116
(2) (104 – 97 + 20) mod 26 + 97 = x; x = 98
(3) (105 – 97 + 18) mod 26 + 97 = x; x = 97
The next step is to do the reverse, namely finding Y:
(1) (Y – 97 + 17) mod 26 + 97 = 116
(2) (Y – 97 + 20) mod 26 + 97 = 98
(3) (Y – 97 + 18) mod 26 + 97 = 97
Yes, I thought so also when I initially came up with this same equation, so I thought that my math is completely incorrect, and I started asking for help, because the program doesn't compute it this way somehow, and I get completely different results. Well, maybe it's some problem with computing the remainder when the quotient is negative. It might be the case. I have to investigate that further.SammyS said:If the % operator does indeed give the remainder,then that should give correct results.
Try this on your test cases.
Use: y = (x – 97 – i)%26 + 97
(1): x = 116, i = 17, should give y = 99 .
y = [(116 – 97 – 17)%26] + 97 = [ 2 % 26] + 97 = 2 + 97 = 99(2) x = 98, i = 20, should give y = 104
y = [(98 – 97 – 20)%26] + 97 = [ (–19) % 26] + 97 = 7 + 97 = 104(3) x = 97, i = 18, should give y = 105
y = [(97 – 97 – 18)%26] + 97 = [ (–18) % 26] + 97 = 8 + 97 = 105
It appears to work just fine.
Yes. I'm not sure about that "%" operation.Vital said:Yes, I thought so also when I initially came up with this same equation, so I thought that my math is completely incorrect, and I started asking for help, because the program doesn't compute it this way somehow, and I get completely different results. Well, maybe it's some problem with computing the remainder when the quotient is negative. It might be the case. I have to investigate that further.
Thank you so much for your huge patience and your help!
Thank you! I have to try this. There is a good thing about all this; I am glad that I am not that bad in the most basic math; I was so desperate that my equation didn't work, and I couldn't understand why, so I came here, and "tortured" you while the problem was in the way the program treats negatives. :-) Again, thank you very much for your help, and I will try the +26 trick. :-)SammyS said:Yes. I'm not sure about that "%" operation.
It appears that the values you are using for " i " also fall in the [ 0, 25 ] interval. If that's the case, and the problem is with computing the remainder for negatives, then adding 26 (or any positive multiple of 26), before applying the "%" operation should solve the problem.
Not tortured too much ! (Most of any torture there was, I inflicted upon myself.)Vital said:Thank you! I have to try this. There is a good thing about all this; I am glad that I am not that bad in the most basic math; I was so desperate that my equation didn't work, and I couldn't understand why, so I came here, and "tortured" you while the problem was in the way the program treats negatives. :-) Again, thank you very much for your help, and I will try the +26 trick. :-)
EDIT: I just did - added 26 to the equation, and, viola, it does the job! :) What a relief! ) Thank you so much! Now I need to understand my mistake in programming - it's not right that the program can't compute such remainders. :-)
It's C. :-) And it might be some issues with negatives, some nuances I still have to learn. But I can't discuss the programming itself, unfortunately, because I promised to the person who helps me with C, not to seek for any help when it comes to programming; so I can't break my promise. :-)SammyS said:Not tortured too much ! (Most of any torture there was, I inflicted upon myself.)
I'm glad to see that you at least got it to work, even if that's with a "trick".
What programming language?
The main issue is with the details of how the % operation is defined.
It has always annoyed me the way % works with negatives in C. Clearly not specified by a mathematician.SammyS said:Yes. I'm not sure about that "%" operation.
Wikipedia has a good description of various conventions for defining the remainder (and quotient).haruspex said:It has always annoyed me the way % works with negatives in C. Clearly not specified by a mathematician.