23
1
Your task is to given two integer numbers, a
and b
calculate the modular multiplicative inverse of a modulo b, if it exists.
The modular inverse of a
modulo b
is a number c
such that ac ≡ 1 (mod b)
. This number is unique modulo b
for any pair of a
and b
. It exists only if the greatest common divisor of a
and b
is 1
.
The Wikipedia page for modular multiplicative inverse can be consulted if you require more information about the topic.
Input and Output
Input is given as either two integers or a list of two integers. Your program should output either a single number, the modular multiplicative inverse that is in the interval 0 < c < b
, or a value indicating there is no inverse. The value can be anything, except a number in the range (0,b)
, and may also be an exception. The value should however be the same for cases in which there is no inverse.
0 < a < b
can be assumed
Rules
- The program should finish at some point, and should solve each test case in less than 60 seconds
- Standard loopholes apply
Test cases
Test cases below are given in the format, a, b -> output
1, 2 -> 1
3, 6 -> Does not exist
7, 87 -> 25
25, 87 -> 7
2, 91 -> 46
13, 91 -> Does not exist
19, 1212393831 -> 701912218
31, 73714876143 -> 45180085378
3, 73714876143 -> Does not exist
Scoring
This is code golf, so the shortest code for each language wins.
This and this are similar questions, but both ask for specific situations.
Sandbox – Halvard Hummel – 2017-08-24T15:41:36.377
Can our output be empty in case there is no such value? – Mr. Xcoder – 2017-08-24T15:51:09.367
@Mr.Xcoder Yes, but in that case it would need to be empty for all cases in which there is no such value – Halvard Hummel – 2017-08-24T15:53:50.570
6It follows from Fermat's Little Theorem that the multiplicative inverse of a, if it exists, can be computed efficiently as a^(phi(b)-1) mod b, where phi is Euler's totient function: phi(p0^k0 * p1^k1 * ...) = (p0-1) * p0^(k0-1) * (p1-1) * p1^(k1-1) * ... Not saying it leads to shorter code :) – ngn – 2017-08-24T16:08:29.737
Can we output a optional type? I'm assuming we can but I just wanted to check.
– Post Rock Garf Hunter – 2017-08-24T16:22:08.873can I take as input [a,-1,b] ? – J42161217 – 2017-08-24T16:31:38.363
1@Jenny_mathy Taking additional input is generally disallowed. – Mr. Xcoder – 2017-08-24T16:32:49.707
@Mr.Xcoder yes but I would beat mathematica's built in which is generally great! – J42161217 – 2017-08-24T16:34:35.047
5I count six answers that seem to be brute forcing, and unlikely to run all test cases in 60 seconds (some of them give a stack or memory error first). – Ørjan Johansen – 2017-08-24T23:28:40.677
1@ngn : You've conflated Fermat's Little Theorem (FLT) with Euler's improvement to it. Fermat did not know about the Euler phi function. Further, FLT and Euler's improvement only apply if gcd(a,b) = 1. Finally, in the form you have written it, "a^(\phi(b)-1) mod b" is congruent to 1, not a^(-1). To get a^(-1), use a^(\phi(b)-2) mod b. – Eric Towers – 2017-08-25T04:49:59.090
1@EricTowers Euler's is a consequence. Regarding "gcd(a,b)=1" - I did say "if it [the inverse] exists". Are you sure about phi(b)-2? – ngn – 2017-08-25T06:45:29.833
@ngn : Hmm... Seems you got me turned around. It's a^(b -2) = a^(-1) mod p in Fermat's theorem and a^(phi(b) -1) = a^(-1) mod p in Euler's. – Eric Towers – 2017-08-25T12:56:57.097
@EricTowers Looks correct now, assuming by "p" you mean "b=p, where p is prime". I admit it would have been more proper, though just as true, to quote Euler's theorem (which is the generalisation to multiple prime factors) instead of Fermat's Little. – ngn – 2017-08-25T13:19:37.337
60 seconds on which machine? – totallyhuman – 2017-08-25T21:10:21.740
@totallyhuman Reasonable normal machine – Halvard Hummel – 2017-08-25T21:12:28.633
So at least @Mr.Xcoder claims that his brute force solution (in Python) does run in less than 60 seconds on a normal machine - barely. Which is exactly what you'd want to avoid in choosing a non-scoring time limit. – Ørjan Johansen – 2017-08-26T17:14:19.013
Why limit the algorithm for just positive results and arguments, when during the computation one see values can be negative, the same the result,and that algorithm it is for all integers (negative positive zero)? – RosLuP – 2017-08-29T09:01:01.193