RE: reverse of modulo (inverse?)

Brian Raven <[email protected]> Tue, 14 Jun 2011 16:10:35 +0100
Newsgroups gmane.comp.lang.perl.active-perl
Message-ID <F4203BE2BBEC5F4AB88CE7503B82EAB002D3CFA348@pcacoexm01.europe.nyx.com>
From: [email protected] [mailto:[email protected]] On Behalf Of [email protected]
Sent: 14 June 2011 14:39
To: [email protected]
Subject: reverse of modulo (inverse?)

> Greetings.
>
> I'm not having much luck finding this, and it's been a long time since college math, so I thought I'd throw it > at you folks and cross my fingers...
>
> I'm looking for an algorithm that reverses the modulo function. That is, if x = y % 256, given x, what is y?

That's not possible, because you have discarded necessary information.

>
> Broader picture is, I'm trying to back-figure a site number from an IP address. E.g., given number x = 12345, > the IP's middle octets ( x/256 and x%256 respectively) come out to 48 and 57 . Is there an algorithm I can put > the numbers 48 and 57 into and have 12345 come out?

Well, that's a different question (assuming that I have understood the last paragraph). If you have both x/256 and x%256, and you know the modulus/divisor was 256, then you have enough to reconstruct the original. For example ...

use strict;
use warnings;

my $mod = 256;
for my $x (@ARGV) {
    my $o1 = int($x / $mod);
    my $o2 = $x % $mod;
    my $orig_x = $o1 * $mod + $o2;
    print "Starting with x=$x and mod=$mod, o1=$o1 o2=$o2, reconstructed x=$orig_x\n";
}

HTH


--
Brian Raven




Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs