admin管理员组文章数量:1406925
I have hard time wrapping my head around how to get this to work so I came to ask the help of the brilliant minds in here.
The thing is, I want to reverse the process of the below equation so that I get X
from the given Y
and Z
.
Z = [ ( X * 30 ) % Y ]
For the use-case, a user inputs number Y
and then presses ENTER, the system get's the current server time and then multiplies that by 30. The user will then be given the remainder of the server time in format HHMMssxxx
, (hmm, xxx
here is the millisecond.. I don't know the format letter for millisecond.. hehe..), divided by Y
- that is (X*30) % Y
where X
is the current server time converted to int.
How can I do this in reverse?
The catch is, X
should not be greater than 2359999
-> (23:59:59.999) the maximum time value for a 24-hour clock.
Supposedly I have Z = 32, Y = 400
, how can I find X
?
I know that it's possible to have multiple answers. Here's what I came up so far but I think this is not very optimal in terms of performance.
function getTimeIDx(rem, codeIndexer) {
var times = [];
for(var i = 0; i < 2400000; i++) {
if((i * 30) % codeIndexer == rem) {
var str = i.toString(),
l = str.length;
if(l < 9)
str = '000000000'.substr(0, 9 - l) + str;
str = str.substr(0, 2) + ':' + str.substr(2, 2) + ':' + str.substr(4, 2) + '.' + str.substr(6);
if(/^(?:[0-1]?\d|2[0-3]):(?:[0-5]?\d):(?:[0-5]+\d)/.test(str))
times.push(str);
}
}
return times;
}
Is there some way to do this more efficiently? Is there something like a inverse modulo?
EDIT:
Updated code to check if the string is a valid time.
I have hard time wrapping my head around how to get this to work so I came to ask the help of the brilliant minds in here.
The thing is, I want to reverse the process of the below equation so that I get X
from the given Y
and Z
.
Z = [ ( X * 30 ) % Y ]
For the use-case, a user inputs number Y
and then presses ENTER, the system get's the current server time and then multiplies that by 30. The user will then be given the remainder of the server time in format HHMMssxxx
, (hmm, xxx
here is the millisecond.. I don't know the format letter for millisecond.. hehe..), divided by Y
- that is (X*30) % Y
where X
is the current server time converted to int.
How can I do this in reverse?
The catch is, X
should not be greater than 2359999
-> (23:59:59.999) the maximum time value for a 24-hour clock.
Supposedly I have Z = 32, Y = 400
, how can I find X
?
I know that it's possible to have multiple answers. Here's what I came up so far but I think this is not very optimal in terms of performance.
function getTimeIDx(rem, codeIndexer) {
var times = [];
for(var i = 0; i < 2400000; i++) {
if((i * 30) % codeIndexer == rem) {
var str = i.toString(),
l = str.length;
if(l < 9)
str = '000000000'.substr(0, 9 - l) + str;
str = str.substr(0, 2) + ':' + str.substr(2, 2) + ':' + str.substr(4, 2) + '.' + str.substr(6);
if(/^(?:[0-1]?\d|2[0-3]):(?:[0-5]?\d):(?:[0-5]+\d)/.test(str))
times.push(str);
}
}
return times;
}
Is there some way to do this more efficiently? Is there something like a inverse modulo?
EDIT:
Updated code to check if the string is a valid time.
Share Improve this question edited Mar 18, 2018 at 14:45 xGeo asked Mar 18, 2018 at 13:24 xGeoxGeo 2,1392 gold badges20 silver badges44 bronze badges 4- If you could reverse it, you would be the most powerful person in the world. – Jonas Wilms Commented Mar 18, 2018 at 13:36
- that question would also be good for math.stackexchange. – Manos Kounelakis Commented Mar 18, 2018 at 13:40
- yeah, thanks.. ill try posting it there. – xGeo Commented Mar 18, 2018 at 14:04
- @JonasW., how about the code above? that will still return something. Looks correct to me though it gives multiple answers. Am I powerful now? :D – xGeo Commented Mar 18, 2018 at 14:04
2 Answers
Reset to default 3You cannot reverse it. Modulo is the remainder from a division operation.
Simplifying your equation. Z = Y % 2
Z is 0 for half of the values and 1 for the rest.
You can not solve for the dividend with just the remainder and the divisor.
Lets fill it into the equation:
32 = ( X * 30 ) % 400
Then this means that X * 30
is a multiple of 400 plus 32:
32
432
832
...
Now we could divide that by 30 to get x. That could be done in js like this:
function* reverse(Z, Y) {
for(let n = 0; ; n++)
yield (Z + Y * n) / 30;
}
Usable as:
for(let X of reverse(32, 400))
console.log(X);
Note that this loop will run forever as there are infinite results. Try it
本文标签: javascriptWhat is the reverse of the Modulo operatorStack Overflow
版权声明:本文标题:javascript - What is the reverse of the Modulo operator - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744345107a2601710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论