Home > Back-end >  PHP reverting a modulo math function
PHP reverting a modulo math function

Time:07-29

I have a function that generates a number with an input in PHP:

$number = 1;    
$result = $number * 683567 % 1000000;
// OUTPUT: 683567

$number = 2;
$result = $number * 683567 % 1000000;
// OUTPUT: 367134

$number = 123;
$result = $number * 683567 % 1000000;
// OUTPUT: 78741

I want to revert this and enter the output values to find result. For example:

$number= 683567;
$result = ___________(function);
// OUTPUT SHOULD BE: 1

$number= 367134;
$result = ___________(function);
// OUTPUT SHOULD BE: 2

$number= 78741;
$result = ___________(function);
// OUTPUT SHOULD BE: 123

I couldn't figure out how to do the function. I would be glad if you help.

CodePudding user response:

I solved this by counting up by steps of 683567 until I find a match that match the result (mod 1000000).

NOTE: This will find the lowest number with the same modulus, it might not be the same as the original. – Barmar

function un_mod($result, $factor = 683567, $mod = 1000000)
{
    $total = $factor;
    $i = 1;
    while (($total % $mod) != $result) {
        $total  = $factor;
        $i  ;

        if ($i>9999) {
            break;
        }

    }
    return $i;
}

echo un_mod(78741);     // 123
  • Related