June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
24
Task/Modular-inverse/C-sharp/modular-inverse.cs
Normal file
24
Task/Modular-inverse/C-sharp/modular-inverse.cs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
public class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
System.Console.WriteLine(42.ModInverse(2017));
|
||||
}
|
||||
}
|
||||
|
||||
public static class IntExtensions
|
||||
{
|
||||
public static int ModInverse(this int a, int m)
|
||||
{
|
||||
if (m == 1) return 0;
|
||||
int m0 = m;
|
||||
(int x, int y) = (1, 0);
|
||||
|
||||
while (a > 1) {
|
||||
int q = a / m;
|
||||
(a, m) = (m, a % m);
|
||||
(x, y) = (y, x - q * y);
|
||||
}
|
||||
return x < 0 ? x + m0 : x;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue