-1

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.

1
  • It's not possible to invert modulo, because there are an infinite set of numbers with the same modulus. Commented Jul 28, 2022 at 23:22

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

This will find the lowest number with the same modulus, it might not be the same as the original.
The function that I give ($number * 683567 % 1000000) generates 1.000.000 different unique numbers according to the number you entered. So your function will always be giving the correct results for me. Thanks. It works great! I just changed "$i>9999" to "$i>1000000".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.